2017-10-15 11:31:44 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-10-27 07:16:36 +08:00
|
|
|
process.on('unhandledRejection', error => {
|
2017-10-23 10:56:42 +08:00
|
|
|
// this makes sure that stacktrace of failed promise is printed out
|
2017-10-27 07:16:36 +08:00
|
|
|
console.log(error);
|
2017-10-26 10:39:21 +08:00
|
|
|
|
|
|
|
// but also try to log it into file
|
2017-10-27 07:16:36 +08:00
|
|
|
require('../services/log').info(error);
|
2017-10-23 10:56:42 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
const app = require('../app');
|
|
|
|
const debug = require('debug')('node:server');
|
2017-11-17 12:55:50 +08:00
|
|
|
const fs = require('fs');
|
2017-10-26 10:39:21 +08:00
|
|
|
const http = require('http');
|
2017-11-17 12:55:50 +08:00
|
|
|
const https = require('https');
|
2017-11-17 12:06:16 +08:00
|
|
|
const config = require('../services/config');
|
|
|
|
const log = require('../services/log');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-17 12:06:16 +08:00
|
|
|
const port = normalizePort(config['Network']['port'] || '3000');
|
2017-10-15 11:31:44 +08:00
|
|
|
app.set('port', port);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create HTTP server.
|
|
|
|
*/
|
2017-11-17 12:55:50 +08:00
|
|
|
let server;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-17 12:55:50 +08:00
|
|
|
if (config['Network']['https']) {
|
|
|
|
const options = {
|
|
|
|
key: fs.readFileSync(config['Network']['keyPath']),
|
|
|
|
cert: fs.readFileSync(config['Network']['certPath'])
|
|
|
|
};
|
|
|
|
|
|
|
|
server = https.createServer(options, app);
|
|
|
|
|
|
|
|
log.info("App HTTPS server starting up at port " + port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
server = http.createServer(app);
|
|
|
|
|
|
|
|
log.info("App HTTP server starting up at port " + port);
|
|
|
|
}
|
2017-11-17 12:06:16 +08:00
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
/**
|
|
|
|
* Listen on provided port, on all network interfaces.
|
|
|
|
*/
|
|
|
|
|
|
|
|
server.listen(port);
|
|
|
|
server.on('error', onError);
|
|
|
|
server.on('listening', onListening);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize a port into a number, string, or false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function normalizePort(val) {
|
2017-10-26 10:39:21 +08:00
|
|
|
const port = parseInt(val, 10);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
|
|
|
return val;
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
|
|
|
return port;
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
return false;
|
2017-10-15 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function onError(error) {
|
2017-10-26 10:39:21 +08:00
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bind = typeof port === 'string'
|
|
|
|
? 'Pipe ' + port
|
|
|
|
: 'Port ' + port;
|
|
|
|
|
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
|
|
|
console.error(bind + ' requires elevated privileges');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EADDRINUSE':
|
|
|
|
console.error(bind + ' is already in use');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw error;
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function onListening() {
|
2017-10-26 10:39:21 +08:00
|
|
|
const addr = server.address();
|
|
|
|
const bind = typeof addr === 'string'
|
|
|
|
? 'pipe ' + addr
|
|
|
|
: 'port ' + addr.port;
|
|
|
|
|
|
|
|
debug('Listening on ' + bind);
|
2017-10-15 11:31:44 +08:00
|
|
|
}
|