使用带有Nodejs / Expressjs的集群时,页面会永远加载

时间:2016-03-03 12:17:27

标签: 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 = 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;
}

这是parrot.js(包含群集)

//import cluster
var cluster = require('cluster');

//startWorker
function startWorker(){
    var worker = cluster.fork();
    console.log('CLUSTER: Worker %d started', worker.id);
}

if(cluster.isMaster){

    //in case the cluster is Master
    require('os').cpus().forEach(function(){
        startWorker();
    });

    cluster.on('disconnect', function(worker){
        console.log('CLUSTER: Worker %d disconnected from the cluster', worker.id);
    });

    cluster.on('exit', function(worker, code, signal){
        console.log('CLUSTER: Worker %d died with exit code %d (%s)', worker.id, code, signal);
        startWorker();
    });

}else{
    //in case cluster.isWorker (not master), run app directly
    require('./app.js')();
}

问题在于,当我运行node app.js时,应用程序在http://localhost:3000上工作正常...并且该页面在浏览器中运行良好。

当我作为一组集群运行时(使用node 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新手。

谢谢

1 个答案:

答案 0 :(得分:0)

我不知道究竟是什么问题,但是当我在另一台计算机上测试(使用32位操作系统)时,上面的例子没有任何问题。

当我在浏览器中访问某个页面时,这是我的结果:

CLUSTER:工人1开始

BOOL MakeWindowTransparent(HWND hWnd, unsigned char factor) {
/* First, see if we can get the API call we need. If we've tried
* once, we don't need to try again. */
if (!initialized) {
  HMODULE hDLL = LoadLibrary(L"user32");

  pSetLayeredWindowAttributes =
    (PSLWA) GetProcAddress(hDLL, "SetLayeredWindowAttributes");

  initialized = TRUE;
}

if (pSetLayeredWindowAttributes == NULL)
  return FALSE;

/* Windows need to be layered to be made transparent. This is done
* by modifying the extended style bits to contain WS_EX_LAYARED. */
SetLastError(0);

SetWindowLong(hWnd,
  GWL_EXSTYLE,
  GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

if (GetLastError())
  return FALSE;

/* Now, we need to set the 'layered window attributes'. This
* is where the alpha values get set. */
return pSetLayeredWindowAttributes(hWnd,
  RGB(255, 255, 255),
  factor,
  LWA_COLORKEY | LWA_ALPHA);

万一集群不适合您,请在其他计算机上进行测试。

我有另一个问题:我不知道为什么群集工作者2(最后一个请求)都提供了所有请求,似乎工作者1没有收到任何请求。

由于