0

This problem i got on my machine only. I tried my code on other machines it works perfectly, I didn't got any solution because there is no code level problem, may be its OS problem or may be Nodejs version problem, I don't know.

Operating Syatem: Window 7 Professional 64-bit

Nodejs version: 4.4.3

Code

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }


    cluster.on('exit', function(worker, code, signal) {
        console.log("worker %s died", code);
        cluster.fork();
    });
} else {
    var express = require('express');
    var http = require('http');

    // init app
    var app = express();

    app.get('/',function(req,res){
        res.send('home page');
    });

    function createServer(app) {
        return http.createServer(app);
    }

    app.locals.server = createServer(app);

    app.locals.server.listen(8080, function() {
        console.info("server online");
    });
}

logs on command line

server online
server online
server online
server online

When i hit http://localhost:8080, then new logs are

worker 3221225477 died
server online

and no data got on browser, continuously loading on browser.

Please help.

Gaurav Kumar Singh
  • 1,550
  • 3
  • 11
  • 31
  • 1
    is port `8080` open on your machine ? Some firewall might blocking connection over that port. – Gaurav Gandhi Mar 27 '17 at 10:03
  • @ErrHunter I'm running it locally and it works fine without cluster, – Gaurav Kumar Singh Mar 27 '17 at 12:33
  • Have you tries using [PM2](https://github.com/Unitech/pm2). Install and try using it's `Cluster mode`, if it works with that then we might have problem with our implementation, if it doesn't work over there too means its some other problem, mostly OS or network or something. – Gaurav Gandhi Mar 27 '17 at 13:01

1 Answers1

0

First of all you didn't define any route, where your server can get the request.

As per your code you can define one route in your else condition

app.get('/',function(req,res){
        res.send('home page');
    })

And hit the URLhttp://localhost:8080.

Hope it works

abdulbarik
  • 6,101
  • 5
  • 38
  • 59