Skip to content

Instantly share code, notes, and snippets.

@foyzulkarim
Created July 14, 2021 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save foyzulkarim/cd2f6ba229efe42307ba6282384c3d53 to your computer and use it in GitHub Desktop.
Save foyzulkarim/cd2f6ba229efe42307ba6282384c3d53 to your computer and use it in GitHub Desktop.
Webinar on Node.js : Express.js handle 200K+ request in 10seconds using Cluster and PM2
const express = require('express')
var numCPUs = require('os').cpus().length;
const cluster = require('cluster');
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
});
const doClustering = false;
if (cluster.isMaster && doClustering) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('death', function (worker) {
logger.info('worker ' + worker.pid + ' died');
});
} else {
app.listen(3000, () => {
console.log('Example app listening on port 3000!')
})
}
{
"name": "fblive",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start:nodemon": "nodemon index.js",
"ac": "autocannon -c 12 http://localhost:3000",
"pm2start": "pm2 start index -i max",
"pm2stop": "pm2 stop index",
"pm2monit": "pm2 monit"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"autocannon": "^7.4.0",
"express": "^4.17.1",
"nodemon": "^2.0.12",
"pm2": "^5.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment