Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda