Node.js:http.Server,http.Agent,套接字和http.request之间的关系
根据文档 :
Node.js为每个服务器维护几个连接来发出HTTP请求。 这个function允许透明地发出请求。
该文档进一步指定Node默认情况下依赖于http.globalAgent
发出请求,但是您可以通过创build一个新的http.Agent
来使用自己的代理。 代理被用来为http请求“池套接字”。
我对这一切的解释是,每当你做一个http.createServer
,默认情况下你会得到几个套接字(大概就是“连接”的意思)来做http请求,这些套接字由http.globalAgent
集中/pipe理。
什么是不清楚的是当你创build自己的http.Agent
会发生什么。 Agent
是否“接pipe”了先前由http.globalAgent
pipe理的http.globalAgent
? 还是你必须通过agent.createConnection
为你的新Agent
创build一个新的套接字?
在相关说明中,如果我要在同一个节点进程中启动两个服务器,然后发出http请求,例如
const server1 = http.createServer(function(req, res) { res.end('Hello from server1'); }).listen(3000); const server2 = http.createServer(function(req, res) { res.end('Hello from server2'); }).listen(5000); http.get('/someurl');
请求从哪个服务器发出? http.Agent
在这里发挥作用吗?