forked from AUT-MD-2017/lbs_ad_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (59 loc) · 1.71 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require('babel-core/register')({
presets: ['es2015', 'stage-0'],
plugins: ['transform-runtime', 'syntax-object-rest-spread'],
});
const _ = require('lodash');
const kit = require('nokit');
const http = require('http');
const chalk = require('chalk');
const morgan = require('morgan');
const express = require('express');
const compression = require('compression');
const bodyPaser = require('body-parser');
const { argv } = require('yargs');
const cfg = require('./config');
const routes = require('./routes');
class Server {
constructor(options) {
const opts = _.extend({
port: cfg.port,
ip: cfg.ip,
}, options);
this.port = opts.port;
this.ip = opts.ip;
this.initApp();
this.initServer();
}
initApp() {
const app = express();
app.disable('x-powered-by');
app.set('views', `${__dirname}/views`);
app.set('view engine', 'ejs');
if (kit.isDevelopment()) {
app.use(morgan('tiny'));
}
// compresses the content in gzip
app.use(compression());
app.use(bodyPaser());
app.use('/static', express.static(`${__dirname}/dist`));
app.use(routes);
this.app = app;
}
initServer() {
const { port, ip } = this;
const server = http.createServer(this.app);
this.server = server.listen(port, ip, () => {
kit.log(`App is running in ${chalk.blue(process.env.NODE_ENV)} environment. Please visit: http://${ip}:${port}`);
});
// https://github.com/remy/nodemon#controlling-shutdown-of-your-script
process.once('SIGUSR2', () => {
this.close();
process.kill(process.pid, 'SIGUSR2');
});
}
close() {
this.server.close();
kit.log(chalk.blue('>> Server closed.'));
}
}
module.export = new Server(argv);