hel*_*ont 2 port scalability node.js pm2 server
我正在学习如何在我设置的小沙箱中扩展服务器。这是非常简单的代码:
\n\n\'use strict\';\n\nconst express = require(\'express\');\nconst bodyParser = require(\'body-parser\');\nconst app = express();\n\nconst instanceId = parseInt(Math.random() * 1000);\n\n//Allow all requests from all domains & localhost\napp.all(\'/*\', function(req, res, next) {\n res.header("Access-Control-Allow-Origin", "*");\n res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept");\n res.header("Access-Control-Allow-Methods", "POST, GET");\n next();\n});\n\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded({extended: false}));\n\napp.get(\'/\', function(req, res) {\n console.log(`[${new Date()}] ${req.method} ${req.originalUrl} from ${req.ip} at ${instanceId}`);\n res.send(`received at ${Date.now()} from ${instanceId}`);\n});\n\napp.listen(6069);\n
Run Code Online (Sandbox Code Playgroud)\n\n没什么疯狂的,只是吐出日期和收到请求的实例。
\n\n用于扩展 Nodejs 服务器的 pm2 文档建议我运行:
\n\npm2 start server.js -i 5\n
Run Code Online (Sandbox Code Playgroud)\n\n效果非常好。这是我使用 npm 模块 loadtest 对其进行压力测试时的示例输出:
\n\nserver-0 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 847\nserver-1 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 261\nserver-3 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 328\nserver-2 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 163\nserver-4 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 351\nserver-0 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 847\nserver-3 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 328\nserver-1 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 261\nserver-2 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 163\nserver-4 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 351\nserver-0 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 847\nserver-3 [Sun Aug 07 2016 00:13:53 GMT-0400 (EDT)] GET / from ::ffff:127.0.0.1 at 328\n
Run Code Online (Sandbox Code Playgroud)\n\n这是我的问题。为什么节点没有抛出端口 6069 正在使用的错误?多个服务器正在尝试使用端口\xe2\x80\x94,但没有抱怨。为什么?
\n 归档时间: |
|
查看次数: |
1721 次 |
最近记录: |