Hey Guys我正在运行这个带有节点的API生成器,
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'));
});
}
这与node thefile.js
完美匹配,并根据需要生成我的端点,但如果我尝试将require()“删除”到我的server.js中,我将问题集成到我的grunt serve
中我最终得到了多个地址使用错误,如下面的
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)
我的server.js的内容是这样的
'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;
我怎样才能让它发挥作用?
答案 0 :(得分:1)
你在server.js和do_api.js中都使用了listen。您只需要开始侦听一次请求。
您遇到此错误,因为您无法从多个进程侦听同一主机和端口。即使您删除了对listen
的重复调用,除非每个集群在其自己的环境中运行,否则这将无效:它们要么侦听不同主机上的相同端口,要么监听同一主机上的不同端口,或者(可能是你想要的)他们在单个主机/端口组合中充当单个请求监听器的工作者。