为什么从nodejs网站获取的此节点集群示例代码在生产中失败?
链接到示例代码:https://nodejs.org/api/cluster.html#cluster_cluster
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < 2; i++) { // changed from numCPUs to 2
cluster.fork();
}
cluster.on('exit', function(worker) {
console.log(`worker ${worker.id} died...`);
cluster.fork(); // reset
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
使用命令node server.js
可以正常运行
但是,如果使用babel build
构建它,然后运行它,则将失败,并显示以下错误。部署到Heroku时也是一样。如何在生产中进行这项工作?
Error: Cannot find module '/app/server/undefined'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3