-
-
Save cb-v4s/8fcef8da0c300d9d505a877d91f38d08 to your computer and use it in GitHub Desktop.
multi-core server manager for an express webserver performance
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
| const cluster = require('cluster') | |
| const express = require('express') | |
| // const compression = require('compression') | |
| const os = require('os') | |
| const app = express() | |
| const port = 3000 | |
| const ncpus = os.cpus().length | |
| // compression middleware for smaller responses and better performance | |
| // app.use( | |
| // compression({ | |
| // level: 6, | |
| // threshold: 10 * 10000, | |
| // filter: (req, res) => { | |
| // if (req.headers['x-no-compression']) return false; | |
| // return compression.filter(req, res); | |
| // } | |
| // }) | |
| // ) | |
| app.get('/', (req, res) => { | |
| res.send('Hello World!') | |
| cluster.worker.kill() | |
| }) | |
| if (cluster.isMaster){ | |
| for(i = 0; i < ncpus; i++){ | |
| cluster.fork() | |
| } | |
| // handle worker events | |
| cluster.on('exit', (worker, code, signal) => { | |
| cluster.fork() | |
| }) | |
| } else { | |
| app.listen(port, () => console.log(`Process ${process.pid} running @ http://localhost:${port}`)) | |
| } | |
| // app.listen(port, () => console.log(`App running @ http://localhost:${port}`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment