Skip to content

Instantly share code, notes, and snippets.

@robinglen
Created September 16, 2018 16:34
Show Gist options
  • Select an option

  • Save robinglen/3eea038b9f0c5e94de73f3c3482fa732 to your computer and use it in GitHub Desktop.

Select an option

Save robinglen/3eea038b9f0c5e94de73f3c3482fa732 to your computer and use it in GitHub Desktop.
Simple hello world in Node, using Fastify and clustering for Medium post
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}`);
});
}
@matt212
Copy link

matt212 commented Aug 7, 2022

@thearegee
Thanks again for detailed explanation .
to answer your question.
I currently dont have any infra setup I was researching
what is best to deploy , maintain and scale a normal b2b nodejs postgres platform and this gist came across :)
here is my nodejs postgres platform
https://github.com/matt212/Nodejs_Postgresql_VanillaJS_Fastify
i would look into docker and K8 as you have stated earlier thanks for that
that being said , Any suggestion pointers to scale the boilerplate platform is welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment