问题描述
我在我的第一个节点项目上做了一点OJT,而我可以站起来一个简单的服务器,应用程序将被锤击,所以使用集群似乎是一个好主意。我已经将我在各种搜索(包括SO)中找到的一些代码片段拼凑在一起,但服务器将无法启动。我确定我没有经验的节点让我做一些愚蠢的事情,但是我看不到它。 var express = require ( '表现' );
var cluster = require('cluster');
var path = require('path');
var cCPUs = require('os').cpus()。length;
var port = 3000;
var root = path.dirname(__dirname);
if(cluster.isMaster){
for(var i = 0; i cluster.fork();
}
cluster.on('death',function(worker){
console.log('Worker'+ worker.pid +'death。');
});
}
else {
// eyes.inspect(process.env);
console.log('Worker:%s',process.env.NODE_WORKER_ID);
var app = express();
var routes = require('./routes')(app);
app
.use(cluster.repl(root +'cluster.repl'))
.use(cluster.stats({connections:true,requests:true}))
.use(cluster.reload(root))
.listen(port);
}
结果:
TypeError:Object#< Cluster>没有方法'repl'
如果我删除使用
调用,工作人员正常启动,但 process.env.NODE_WORKER_ID
是 undefined
。检查 process.env
显示我绝对没有定义。也许我使用的代码片段是旧版本,但我不知道如何以任何其他方式识别工作线程。
如果任何人可以解开任何我'我真的很感激,
对于以后搜索的人来说,这是我最终的结论: >
var cluster = require('cluster');
var express = require('express');
var path = require('path');
var port = 3000;
var root = path.dirname(__dirname);
var cCPUs = require('os')。cpus()。length;
if(cluster.isMaster){
//为每个CPU创建一个worker
(var i = 0; i cluster.fork();
}
cluster.on('online',function(worker){
console.log('Worker'+ worker.process.pid +'在线) ;
});
cluster.on('exit',function(worker,code,signal){
console.log('worker'+ worker.process.pid +'death。');
} );
}
else {
var app = express();
var routes = require('./routes')(app);
app
.use(express.bodyParser())
.listen(port);
}
我仍然很早在节点学习曲线,但服务器启动并且似乎在每个核心上都有运行。感谢JohnnyH让我走上正轨。
I'm doing a little OJT on my first node project and, while I can stand up a simple server, the app is going to get hammered so using cluster seems like a good idea. I've cobbled together some code snippets that I've found in various searches (including SO), but the server won't start. I'm sure my inexperience with node has me doing something stupid, but I don't see it.
var express = require( 'express' );
var cluster = require( 'cluster' );
var path = require( 'path' );
var cCPUs = require( 'os' ).cpus().length;
var port = 3000;
var root = path.dirname( __dirname );
if( cluster.isMaster ) {
for( var i = 0; i < cCPUs; i++ ) {
cluster.fork();
}
cluster.on( 'death', function( worker ) {
console.log( 'Worker ' + worker.pid + ' died.' );
});
}
else {
// eyes.inspect( process.env );
console.log( 'Worker: %s', process.env.NODE_WORKER_ID );
var app = express();
var routes = require( './routes' )( app );
app
.use( cluster.repl( root + 'cluster.repl' ) )
.use( cluster.stats({ connections: true, requests: true }) )
.use( cluster.reload( root ) )
.listen( port );
}
RESULT:
TypeError: Object #<Cluster> has no method 'repl'
If I remove the use
calls, the workers start up correctly, but process.env.NODE_WORKER_ID
is undefined
. Inspecting process.env
shows me that it's definitely not defined. Maybe the snippet I used was from an old version, but I'm not sure how to identify the worker thread in any other way.
If anyone can unscrambled whatever I've scrambled, I'd really appreciate it.
For anyone searching later, here's what I ended up with:
var cluster = require( 'cluster' );
var express = require( 'express' );
var path = require( 'path' );
var port = 3000;
var root = path.dirname( __dirname );
var cCPUs = require('os').cpus().length;
if( cluster.isMaster ) {
// Create a worker for each CPU
for( var i = 0; i < cCPUs; i++ ) {
cluster.fork();
}
cluster.on( 'online', function( worker ) {
console.log( 'Worker ' + worker.process.pid + ' is online.' );
});
cluster.on( 'exit', function( worker, code, signal ) {
console.log( 'worker ' + worker.process.pid + ' died.' );
});
}
else {
var app = express();
var routes = require( './routes' )( app );
app
.use( express.bodyParser() )
.listen( port );
}
I'm still very early in the node learning curve, but the server starts and appears to have a working running on each core. Thanks to JohnnyH for getting me on the right track.
这篇关于在Expressjs应用程序中使用集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!