Created
September 23, 2018 10:44
-
-
Save DanishSiddiq/08c12dce3d320eede7a47905dcb58fad to your computer and use it in GitHub Desktop.
app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'http'; | |
import morgan from 'morgan'; | |
import express from 'express'; | |
import bodyParser from 'body-parser'; | |
import cluster from 'cluster'; | |
import { setRouter } from './route'; | |
const app = express(); | |
let workers = []; | |
/** | |
* Setup number of worker processes to share port which will be defined while setting up server | |
*/ | |
const setupWorkerProcesses = () => { | |
// to read number of cores on system | |
let numCores = require('os').cpus().length; | |
console.log('Master cluster setting up ' + numCores + ' workers'); | |
// iterate on number of cores need to be utilized by an application | |
// current example will utilize all of them | |
for(let i = 0; i < numCores; i++) { | |
// creating workers and pushing reference in an array | |
// these references can be used to receive messages from workers | |
workers.push(cluster.fork()); | |
// to receive messages from worker process | |
workers[i].on('message', function(message) { | |
console.log(message); | |
}); | |
} | |
// process is clustered on a core and process id is assigned | |
cluster.on('online', function(worker) { | |
console.log('Worker ' + worker.process.pid + ' is listening'); | |
}); | |
// if any of the worker process dies then start a new one by simply forking another one | |
cluster.on('exit', function(worker, code, signal) { | |
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal); | |
console.log('Starting a new worker'); | |
cluster.fork(); | |
workers.push(cluster.fork()); | |
// to receive messages from worker process | |
workers[workers.length-1].on('message', function(message) { | |
console.log(message); | |
}); | |
}); | |
}; | |
/** | |
* Setup an express server and define port to listen all incoming requests for this application | |
*/ | |
const setUpExpress = () => { | |
// create server | |
app.server = http.createServer(app); | |
// logger | |
app.use(morgan('tiny')); | |
// parse application/json | |
app.use(bodyParser.json({ | |
limit: '2000kb', | |
})); | |
app.disable('x-powered-by'); | |
// routes | |
setRouter(app); | |
// start server | |
app.server.listen('8000', () => { | |
console.log(`Started server on => http://localhost:${app.server.address().port} for Process Id ${process.pid}`); | |
}); | |
// in case of an error | |
app.on('error', (appErr, appCtx) => { | |
console.error('app error', appErr.stack); | |
console.error('on url', appCtx.req.url); | |
console.error('with headers', appCtx.req.headers); | |
}); | |
}; | |
/** | |
* Setup server either with clustering or without it | |
* @param isClusterRequired | |
* @constructor | |
*/ | |
const setupServer = (isClusterRequired) => { | |
// if it is a master process then call setting up worker process | |
if(isClusterRequired && cluster.isMaster) { | |
setupWorkerProcesses(); | |
} else { | |
// to setup server configurations and share port address for incoming requests | |
setUpExpress(); | |
} | |
}; | |
setupServer(true); | |
export { app }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice simple server.
Is line 42 duplicated with 43 ?