Skip to content

Application for handle single thread nodejs, using fork cpu in device

License

Notifications You must be signed in to change notification settings

dhianica/cluster-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cluster-nodejs

Application for handle single thread nodejs

For handle single thread nodejs you can use cluster in library nodejs

if (cluster.isMaster) {
  // Fork workers.
  for (let i = 0; i < totalCPUs; i++) {
    cluster.fork()
  }
  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`)
    console.log("Let's fork another worker!")
    cluster.fork()
  })
} else {

  app = express()

  app.use(cors())
  app.use(express.urlencoded({ extended: false, parameterLimit: 10000 }))
  app.use(express.json())

  const IP = config.env.ip
  const PORT = config.env.port
  app.listen(PORT, () => {
    console.info(`Application date & time starting----@ ${moment().format('YYYY-MM-DD HH:mm:ss')}`)
    console.info(`API server ip & port running--------@ http://${IP}:${PORT}`)
  })

  app.get('/', async (req, res) => {
    try {
      res.send('Success to connect!')
    } catch (error) {
      res.send(`Failed to connect! with error : ${error.message}`)
    }
  })

  app.use('/api', routes)
}

About

Application for handle single thread nodejs, using fork cpu in device

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published