【发布时间】:2017-11-04 19:07:39
【问题描述】:
我在学习 here 的教程时,注意到 example code 的索引文件 (app.js) 与通常的 Express.js 应用程序完全不同。代码封装在集群模块中如下:
// Include the cluster module
var cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for terminating workers
cluster.on('exit', function (worker) {
// Replace the terminated workers
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..
}
这有必要吗,或者我可以在没有集群模块的情况下部署常规代码,如下所示:
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..
谢谢,
【问题讨论】:
标签: javascript node.js amazon-web-services cluster-computing amazon-elastic-beanstalk