Javascript 使用带有Nodejs/Expressjs的集群时,页面将永远加载
这是我的应用程序:app.jsJavascript 使用带有Nodejs/Expressjs的集群时,页面将永远加载,javascript,node.js,express,cluster-computing,Javascript,Node.js,Express,Cluster Computing,这是我的应用程序:app.js /** Express **/ var express = require('express'); /** Create express application **/ var app = express(); /** Set application port **/ app.set('port', process.env.PORT || 3000); /** Set application view engine**/ var handlebars = req
/** Express **/
var express = require('express');
/** Create express application **/
var app = express();
/** Set application port **/
app.set('port', process.env.PORT || 3000);
/** Set application view engine**/
var handlebars = require('express-handlebars').create({
defaultLayout: 'main',
helpers: {
section: function(name, options){
if(!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null;
},
parrot: function(options){
return options.fn(this) + ' <b> parrot </b>';
}
}
});
/*** Cluster Logger**/
app.use(function(req, res, next){
var cluster = require('cluster');
if(cluster.isWorker) console.log('CLUSTER: Worker %d received request.', cluster.worker.id);
next();
});
/** home page**/
app.get('/', function(req, res){
res.send('Welcome !!');
});
/** about page**/
app.get('/about', function(req, res){
res.send('About us!');
});
/** contact page **/
app.get('/contact', function(req, res){
res.send('contact us here');
});
// startServer in export/direct mode
function startServer(){
app.listen(app.get('port'), function(){
console.log('Parrot started in '+app.get('env')+' mode on http://localhost:'+
app.get('port')+
'; \n press Ctrl-C to terminate');
});
}
if(require.main === module){
startServer();
}else{
module.exports = startServer;
}
问题是,当我运行node app.js
时,应用程序在http://localhost:3000
。。。而且页面在浏览器中运行良好
当我作为一组集群运行(使用节点parrot.js
)时,控制台中的一切看起来都很好:
CLUSTER: Worker 1 started
CLUSTER: Worker 2 started
Parrot started in development mode on http://localhost:3000;
press Ctrl-C to terminate
Parrot started in development mode on http://localhost:3000;
press Ctrl-C to terminate
但是,页面永远加载,而浏览器上什么也不显示?我不知道这里有什么问题。很抱歉我的语言,因为我是Node.js新手
谢谢你我不知道到底是什么问题,但是当我在另一台计算机上测试(使用32位操作系统)时,上面的示例没有任何问题 这是我现在在浏览器中访问页面时的结果: 群集:工作进程1已启动
CLUSTER: Worker 2 started
Parrot started in development mode on http://localhost:3333;
press Ctrl-C to terminate
Parrot started in development mode on http://localhost:3333;
press Ctrl-C to terminate
CLUSTER: Worker 2 received request.
CLUSTER: Worker 2 received request.
万一集群不适合您,请在另一台机器上测试
我还有另一个问题:我不知道为什么所有请求都由集群工作者2(最后一个启动的)提供,工作者1似乎没有收到任何请求
谢谢我不知道到底是什么问题,但是当我在另一台计算机上测试(使用32位操作系统)时,上面的示例没有任何问题 这是我现在在浏览器中访问页面时的结果: 群集:工作进程1已启动
CLUSTER: Worker 2 started
Parrot started in development mode on http://localhost:3333;
press Ctrl-C to terminate
Parrot started in development mode on http://localhost:3333;
press Ctrl-C to terminate
CLUSTER: Worker 2 received request.
CLUSTER: Worker 2 received request.
万一集群不适合您,请在另一台机器上测试
我还有另一个问题:我不知道为什么所有请求都由集群工作者2(最后一个启动的)提供,工作者1似乎没有收到任何请求
谢谢