javascript 使用 Express 时如何在 Node.js 中设置 maxSockets?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10307837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:24:14  来源:igfitidea点击:

How do you set maxSockets in Node.js when using Express?

javascriptnode.jsexpress

提问by James Simpson

Is there a way to modify the Node.js maxSocketssetting when using the Express framework?

有没有办法在使用 Express 框架时修改 Node.js maxSockets设置?

回答by danmactough

Somewhere after you do var http = require('http'), just add http.globalAgent.maxSockets = x(where 'x' is the number of sockets you want).

在你做之后的某个地方var http = require('http'),只需添加http.globalAgent.maxSockets = x(其中“x”是你想要的套接字数)。

Please note that if you are making requests over https, you will need to set maxSockets for https as well.

请注意,如果您通过 https 发出请求,则还需要为 https 设置 maxSockets。

var https = require('https');
https.globalAgent.maxSockets = your_val_here;

回答by MosheZada

From version v0.12.0 maxSockets set to Infinity

从版本 v0.12.0 maxSockets 设置为 Infinity

maxSockets are no longer limited to 5. The default is now set to Infinity with the developer and the operating system given control over how many simultaneous connections an application can keep open to a given host.

maxSockets 不再限制为 5。默认值现在设置为 Infinity,开发人员和操作系统可以控制应用程序可以对给定主机保持打开的同时连接数。

Node v0.12.0 (Stable) release notes

Node v0.12.0(稳定版)发行说明