Why don't run at multiple thread?

35 views Asked by At
const express=require("express");
const app=express();
const cluster=require('cluster');
const os=require('os');

function delay(duration){
    const start=Date.now();
    while(Date.now()-start<duration){

    }
}

app.get('/',(req,res)=>{
    res.status(200).send(`request running at id ${process.pid}`)
})


app.get('/timer',(req,res)=>{
    delay(5000)
    res.status(200).send(`Timer running at id ${process.pid}`)
})



if(cluster.isPrimary){
    console.log(`Master running...`)
    const NUM_WORKERS=os.cpus().length
    for(let i=0;i<NUM_WORKERS;i++){
        cluster.fork()
    }
}
else if (cluster){
    console.log(`Fork running at ${process.pid}`)
    app.listen(8000,()=>{
        console.log(`Server is running at port ${8000}`)
    })
}


why they are running at the same pid, thread ? When I make request with get / and /timer they should run at different thread. I tried chatGPT Devin but nothing...

0

There are 0 answers