'Multi Threading Using Cluster Module in node.js - How to use Cluster module when we have controller routes architecture?

I am Using architecture in which I have controllers route and then finally using it in server.js file. I know how to implement cluster modules when all the code is inside server.js file. But unable to use the cluster module in controller-route architecture.

server.js

const express=require('express');
const cluster=require('cluster');
const os=require('os');
const blogRoutes=require('./routes/blog');
const numCpu=os.cpus().length;

const app=express();
if (process.env.NODE_ENV === 'development') {
    app.use(cors({ origin: `${process.env.CLIENT_URL}` }));
} 
app.use('/api',blogRoutes);


if(cluster.isMaster){
for(let i=0;i<numCpu;i++){
    cluster.fork()
}
cluster.on('exit',(worker,code,signal)=>{
    console.log(`worker ${worker.process.pid} died`)
})
}else{
app.listen(port,()=>{console.log(`server is running ${process.pid} on port ${port}`)});
}

This is the code that I wrote in server.js file. This worked to start 4 threads but I am unable to kill the thread as it requires to pass kill method inside route.

routes/blog.js

const express = require('express');
const router = express.Router();
const { create, list} = require('../controllers/blog');
const { requireSignin, adminMiddleware} = require('../controllers/auth');



router.post('/blog', requireSignin, adminMiddleware, create);
router.get('/blogs',list);


module.exports = router;

Please help to implement cluster in this.Thankyou



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source