0
votes

I tried this as my first code and i am getting error

   const cluster = require('cluster');
    const http = require('http');
    const numCPUs = 4;

    if (cluster.isMaster) {
      // Fork workers.
      for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
      }
       cluster.on('online', function(worker) {
            console.log('Worker ' + worker.process.pid + ' is online');
        });

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

    } else {
      // Workers can share any TCP connection
      // In this case it is an HTTP server
      http.createServer((req, res) => {
        res.writeHead(200);
        res.end('hello world\n');
      }).listen(8000);
    }

This is the Error i am getting :-

Worker 11056 is online Worker 11057 is online Worker 11058 is online Worker 11059 is online events.js:141 throw er; // Unhandled 'error' event ^

Error: bind EADDRINUSE null:8000 at Object.exports._errnoException (util.js:870:11) at exports._exceptionWithHostPort (util.js:893:20) at cb (net.js:1302:16) at rr (cluster.js:594:14) at Worker. (cluster.js:564:9) at process. (cluster.js:714:8) at emitTwo (events.js:92:20) at process.emit (events.js:172:7) at handleMessage (internal/child_process.js:689:10) at Pipe.channel.onread (internal/child_process.js:440:11) events.js:141 throw er; // Unhandled 'error' event ^

Error: bind EADDRINUSE null:8000 at Object.exports._errnoException (util.js:870:11) at exports._exceptionWithHostPort (util.js:893:20) at cb (net.js:1302:16) at rr (cluster.js:594:14) at Worker. (cluster.js:564:9) at process. (cluster.js:714:8) at emitTwo (events.js:92:20) at process.emit (events.js:172:7) at handleMessage (internal/child_process.js:689:10) at Pipe.channel.onread (internal/child_process.js:440:11) events.js:141 throw er; // Unhandled 'error' event ^

Error: bind EADDRINUSE null:8000 at Object.exports._errnoException (util.js:870:11) at exports._exceptionWithHostPort (util.js:893:20) at cb (net.js:1302:16) at rr (cluster.js:594:14) at Worker. (cluster.js:564:9) at process. (cluster.js:714:8) at emitTwo (events.js:92:20) at process.emit (events.js:172:7) at handleMessage (internal/child_process.js:689:10) at Pipe.channel.onread (internal/child_process.js:440:11) worker 11056 died worker 11057 died worker 11058 died events.js:141 throw er; // Unhandled 'error' event ^

Error: bind EADDRINUSE null:8000 at Object.exports._errnoException (util.js:870:11) at exports._exceptionWithHostPort (util.js:893:20) at cb (net.js:1302:16) at rr (cluster.js:594:14) at Worker. (cluster.js:564:9) at process. (cluster.js:714:8) at emitTwo (events.js:92:20) at process.emit (events.js:172:7) at handleMessage (internal/child_process.js:689:10) at Pipe.channel.onread (internal/child_process.js:440:11) worker 11059 died

2

2 Answers

1
votes

Are you starting your server somewhere else in your project?

Try splitting your new worker creation out into a separate file.

worker.js

import express from 'express';
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);

And directing server setup to this file

setup.js

const cluster = require('cluster');
const http = require('http');
const numCPUs = 4;

cluster.setupMaster({
  exec: __dirname + '/worker.js'
});

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
     cluster.fork();
  }
  cluster.on('online', function(worker) {
     console.log('Worker ' + worker.process.pid + ' is online');
   });

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

EADDRINUSE means that something else is already bound to the port/IP address combination that you are trying to use.

Things to check:

  • Do you already have some other process binding to port 8000? If so, perhaps change the port number in your code here.

  • Do you have another instance of this code running? (On UNIX-like operating systems, perhaps use ps with appropriate options to find it.) If so, terminate the other instance. (On UNIX-like operating systems, you might use kill or pkill with appropriate options.)