-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (69 loc) · 2.36 KB
/
app.js
File metadata and controls
83 lines (69 loc) · 2.36 KB
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
75
76
77
78
79
80
81
82
83
var express = require('express');
var args = process.argv.join('|');
var port = /\-\-port\|(\d+)(?:\||$)/.test(args) ? ~~RegExp.$1 : 8080;
var https = /\-\-https\|(true)(?:\||$)/.test(args) ? !!RegExp.$1 : false;
var path = require('path');
var DOCUMENT_ROOT = path.resolve(/\-\-root\|(.*?)(?:\||$)/.test(args) ? RegExp.$1 : process.cwd());
var bodyParser = require('body-parser'), cookieParser = require('cookie-parser');
var app = global.app = express();
app.disable('view cache');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use(cookieParser());
var STATIC_ROOT = path.join(DOCUMENT_ROOT, 'static');
app.use(require('./dev-tool.js')(DOCUMENT_ROOT, STATIC_ROOT));
// 静态文件输出
app.use(express.static(STATIC_ROOT));
// utf8 support
app.use(function(req, res, next) {
// attach utf-8 encoding header to text files.
if (/\.(?:js|json|text|css)$/i.test(req.path)) {
res.charset = 'utf-8';
}
next();
});
// 错误捕获。
app.use(function(err, req, res, next) {
console.log(err);
});
// Bind to a port
var fs = require('fs');
var path = require('path');
var server;
if (https) {
server = require('https').createServer({
key: fs.readFileSync(path.join(__dirname, 'key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'), 'utf8'),
}, app);
} else {
server = require('http').createServer(app);
}
server.listen(port, '0.0.0.0', function() {
console.log(' Listening on ' + (https ? 'https' : 'http') + '://127.0.0.1:%d', port);
});
// 在接收到关闭信号的时候,关闭所有的 socket 连接。
(function() {
var sockets = [];
server.on('connection', function (socket) {
sockets.push(socket);
socket.on('close', function() {
var idx = sockets.indexOf(socket);
~idx && sockets.splice(idx, 1);
});
});
var finalize = function() {
// Disconnect from cluster master
process.disconnect && process.disconnect();
process.exit(0);
}
// 关掉服务。
process.on('SIGTERM', function() {
console.log(' Recive quit signal in worker %s.', process.pid);
sockets.length ? sockets.forEach(function(socket) {
socket.destroy();
finalize();
}): server.close(finalize);
});
})(server);