2016-03-03 31 views
1

集羣時,這是我的應用程序:app.js頁面加載永遠與使用的NodeJS/Expressjs

/** 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新手。

謝謝

回答

0

我不知道到底出了什麼問題呢,但是當我在另一臺計算機(與32位操作系統)上測試,上面的例子中工作沒有任何問題。

這是我的結果,現在,當我訪問一個頁面在瀏覽器:

CLUSTER:工人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沒有收到任何請求。

謝謝