2024-06-22 08:09:40 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2024-06-22 14:08:05 -04:00
|
|
|
const app = require('../app')
|
|
|
|
const debug = require('debug')('message:server')
|
|
|
|
const http = require('http')
|
2024-06-22 08:09:40 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get port from environment and store in Express.
|
|
|
|
*/
|
2024-06-22 14:08:05 -04:00
|
|
|
const port = normalizePort('3010')
|
2024-06-22 08:09:40 -04:00
|
|
|
app.set('port', port);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create HTTP server.
|
|
|
|
*/
|
2024-06-22 14:08:05 -04:00
|
|
|
const server = http.createServer(app)
|
2024-06-22 08:09:40 -04: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) {
|
2024-06-22 14:08:05 -04:00
|
|
|
const port = parseInt(val, 10)
|
2024-06-22 08:09:40 -04:00
|
|
|
|
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function onError(error) {
|
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-06-22 14:08:05 -04:00
|
|
|
const bind = typeof port === 'string'
|
2024-06-22 08:09:40 -04:00
|
|
|
? 'Pipe ' + port
|
2024-06-22 14:08:05 -04:00
|
|
|
: 'Port ' + port
|
2024-06-22 08:09:40 -04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function onListening() {
|
2024-06-22 14:08:05 -04:00
|
|
|
const addr = server.address()
|
|
|
|
const bind = typeof addr === 'string'
|
2024-06-22 08:09:40 -04:00
|
|
|
? 'pipe ' + addr
|
2024-06-22 14:08:05 -04:00
|
|
|
: 'port ' + addr.port
|
2024-06-22 08:09:40 -04:00
|
|
|
debug('Listening on ' + bind);
|
|
|
|
}
|