-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
57 lines (45 loc) · 1.46 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
'use strict';
const config = require('wild-config');
const log = require('npmlog');
const http = require('http');
const db = require('./lib/db');
const logserver = require('./lib/logserver');
const port = config.port;
const host = config.host;
log.level = config.log.level;
db.init(err => {
if (err) {
throw err;
}
const app = require('./app'); // eslint-disable-line global-require
app.set('port', port);
let server = http.createServer(app);
server.on('error', err => {
if (err.syscall !== 'listen') {
throw err;
}
let bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// handle specific listen errors with friendly messages
switch (err.code) {
case 'EACCES':
log.error('Express', '%s requires elevated privileges', bind);
return process.exit(1);
case 'EADDRINUSE':
log.error('Express', '%s is already in use', bind);
return process.exit(1);
default:
throw err;
}
});
server.on('listening', () => {
let addr = server.address();
let bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
log.info('Express', 'WWW server listening on %s', bind);
});
process.on('uncaughtException', err => {
log.error('App', err);
});
logserver(() => {
server.listen(port, host);
});
});