I am trying to implement fork and cluster module and below is my code.
const cluster = require("node:cluster");
const http = require("node:http");
const OS = require("node:os");
console.log(OS.cpus().length);
if(cluster.isMaster){
console.log(`Master process ${process.pid} is running`);
cluster.fork()
cluster.fork()
}else{
console.log(`Worker ${process.pid} started`);
const server = http.createServer((req, res)=>{
if(req.url === "/"){
res.writeHead(200, {"content-type": "text/plain"});
res.end("Home Page");
}
else if (req.url === "/slow-page"){
for(let i=0; i < 6000000000; i++){}
res.writeHead(200, {"content-type": "text/plain"});
res.end("Slow Page");
}
});
server.listen(8000, ()=> console.log("Server is running on port 8000"));
}
Ideally, while reload both home page and slow page from above code, home page should load quickly than slow page, but in my case it is not working as expected.
Can some one help me what changes should require in my code?
Ref. : https://www.youtube.com/watch?v=SHR-KmfRIsU&list=PLC3y8-rFHvwh8shCMHFA5kWxD9PaPwxaY&index=61