Forked from robinglen/hello-world-node-fastify-clustered.js
Created
February 3, 2020 09:12
-
-
Save aalfiann/099896dc9c634d0616490c934e9359ce to your computer and use it in GitHub Desktop.
Simple hello world in Node, using Fastify and clustering for Medium post
This file contains 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 numCPUs = require('os').cpus().length; | |
const fastify = require('fastify')(); | |
const port = 8123; | |
if (cluster.isMaster) { | |
console.log(`Master ${process.pid} is running`); | |
for (let i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('exit', worker => { | |
console.log(`Worker ${worker.process.pid} died`); | |
}); | |
} else { | |
fastify.get('*', (req, res) => { | |
res.send('Hello World'); | |
}); | |
fastify.listen(port, () => { | |
console.log(`Fastify "Hello World" listening on port ${port}, PID: ${process.pid}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment