Question

Hey Guys I am running this an API generator with node with this,

var cluster = require('cluster');

if (cluster.isMaster) {

  // first set up logging for the master
  var logger = require("./api/helpers/logging.js")();

  var num_cpus = require('os').cpus().length;
  logger.debug(num_cpus + " <-- that many CPUs detected!");

  // create a fork for each CPU (or "thread" for Intel CPUs...)
  for (var i = 0; i < num_cpus; i++) {
    cluster.fork();
  }

  // if one of the forks dies, spawn a replacement!
  cluster.on('exit', function (worker, code, sig) {
    var report = ('Worker '+worker.process.pid+' died... Creating a new one!\n');
    report += ('  -- code: '+code+'\n');
    report += ('  -- sig: '+sig+'\n');
    logger.warn(report);
    cluster.fork();
  });

  cluster.on('uncaughtException', function(error) {
    logger.error(error);
    process.exit();
  });

}
else {

  var http = require("http");
  var app = require("./api/do_api.js");
  http.createServer(app).listen(app.get('port'), function () {
//    console.log('Express server listening on port ' + app.get('port'));
  });
}

This runs perfectly with node thefile.js and generates my endpoints as needed, though I am having an issue integrating it into my grunt serve if I try just "dropping" the require() into my server.js I end up with multiple address in use errors such as the following

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: bind EADDRINUSE
    at errnoException (net.js:901:11)
    at net.js:1073:26
    at Object.25:1 (cluster.js:587:5)
    at handleResponse (cluster.js:171:41)
    at respond (cluster.js:192:5)
    at handleMessage (cluster.js:202:5)
    at process.EventEmitter.emit (events.js:117:20)
    at handleMessage (child_process.js:318:10)
    at child_process.js:392:7
    at process.handleConversion.net.Native.got (child_process.js:91:7)
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
debugger listening on port 5858
connect.limit() will be removed in connect 3.0
debugger listening on port 5858

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: bind EADDRINUSE
    at errnoException (net.js:901:11)
    at net.js:1073:26
    at Object.23:1 (cluster.js:587:5)
    at handleResponse (cluster.js:171:41)
    at respond (cluster.js:192:5)
    at handleMessage (cluster.js:202:5)
    at process.EventEmitter.emit (events.js:117:20)
    at handleMessage (child_process.js:318:10)
    at child_process.js:392:7
    at process.handleConversion.net.Native.got (child_process.js:91:7)

Contents of my server.js are like this

'use strict';

var express  = require('express'),
    path     = require('path'),
    fs       = require('fs'),
    mongoose = require('mongoose');

/**
 * Main application file
 */

// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

// Application Config
var config = require('./lib/config/config');

// Connect to database
var db = mongoose.connect(config.mongo.uri, config.mongo.options);

// Bootstrap models
var modelsPath = path.join(__dirname, 'lib/models');
fs.readdirSync(modelsPath).forEach(function (file) {
  if (/(.*)\.(js$|coffee$)/.test(file)) {
    require(modelsPath + '/' + file);
  }
});

// Populate empty DB with sample data
require('./lib/config/dummydata');

// Passport Configuration
var passport = require('./lib/config/passport');

var app = express();

// Express settings
require('./lib/config/express')(app);

// Routing
require('./lib/routes')(app);

require('./thefile.js');  // this is the kicker file shown above

// Start server
app.listen(config.port, function () {
  console.log('Express server listening on port %d in %s mode', config.port, app.get('env'));
});

// Expose app
exports = module.exports = app;

How can I get this to work?

Was it helpful?

Solution

You're using listen both in server.js and do_api.js. You need to start listening for requests only once.

You have this error because you can't listen on the same host and port from more than one process. Even if you removed the duplicate call to listen, this won't work unless each cluster runs in its own environment: they either listen to the same port on different hosts, or they listen to different ports on the same host, or (probably what you want) they serve as workers for a single request listener in a single host/port combination.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top