Skip to content

Instantly share code, notes, and snippets.

@GautamPanickar
Last active September 5, 2022 05:20
Show Gist options
  • Save GautamPanickar/f23a474ec9b19bea485b7d680d6197b3 to your computer and use it in GitHub Desktop.
Save GautamPanickar/f23a474ec9b19bea485b7d680d6197b3 to your computer and use it in GitHub Desktop.
import * as HTTP from 'http';
import App from './app';
import DatabaseService from './services/db-service';
const PORT = 66666;
App.set('port', PORT);
console.log('Server listening on port : ' + PORT);
const server = HTTP.createServer(App);
server.listen(PORT);
server.on('error', onError);
server.on('listening', onListening);
const db = new DatabaseService();
db.initializeDB();
/**
* On the event of an error.
* @param error
*/
function onError(error: NodeJS.ErrnoException): void {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof PORT === 'string' ? 'Pipe ' + PORT : 'Port ' + PORT;
switch (error.code) {
case 'EACCES':
console.log(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.log(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
/**
* On listening to port.
*/
function onListening(): void {
const addr = server.address();
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
console.log(`Listening on ${bind}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment