youtube-maze/bin/www.mjs

70 lines
1.4 KiB
JavaScript
Executable File

import http from 'http';
import debug from 'debug';
const log = debug('youtube-maze:server');
/**
* Normalize a port into a number, string, or false.
*/
const normalizePort = val =>
{
const port = parseInt(val, 10);
// Named pipe
if (isNaN(port))
{
return val;
}
// Port number
if (port >= 0)
{
return port;
}
return false;
};
// Instantiate app
import app from '../app.mjs';
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
// Create HTTP server
const server = http.createServer(app);
server.listen(port);
server.keepAliveTimeout = 60000;
server.on('error', error => {
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);
case 'EADDRINUSE':
console.error(`${bind} is already in use`);
process.exit(1);
default:
throw error;
}
});
server.on('listening', () => {
const addr = server.address();
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `port ${addr.port}`;
log(`Listening on ${bind}`);
});