Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
How do I know or how do I run a Node.js server on different CPU cores?

Say my pc has a cpu with 2 cores, and my node.js application using express. How can i run this application on 1 core and this same application on the other core?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Nicovank is right. You are interested in feature called cluster. In nodejs you getting number of cores by this code:

const numCPUs = require('os').cpus().length;

Please refer to this section of documentation: https://nodejs.org/dist/latest-v7.x/docs/api/cluster.html#cluster_cluster. The have great examples.

about 4 years ago · Santiago Trujillo Report

0

Here's a basic example of an express server running in multiple cores using the cluster module.

const cluster = require('cluster');
//Get number of CPUs
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork one worker per core.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });

} else {
    //Worker code...

    const express = require('express');
    const app = express();

    app.get('/', function (req, res) {
        res.send('Hello World!');
    });

    // Bind to a port
    app.listen(8080, () => {
        console.log(`[PID=${process.pid}] server started!`);
    });

}

There are some third party modules too:

  • Cluster2
  • PM2 (Node process manager that has cluster mode)
  • StrongLoop - strong-cluster-control

You can also check:

Node.js on multi-core machines

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!