我希望创建到主机的持久http连接(api.development.push.apple.com),并为许多路径发送POST请求(例如,'/3/device/1‘、'/3/device/2’等)。下面的代码是为每个http.request()创建一个与主机的连接还是多个连接?
var http = require('http');
http.request({
host: 'api.development.push.apple.com',
port: 443,
path: '/3/device/1',
method: 'POST',
}).end();
http.request({
host: 'api.development.push.apple.com',
port: 443,
path: '/3/device/2',
method: 'POST'
}).end();发布于 2016-07-27 13:48:17
您希望对所有请求使用相同的代理。
如果没有在options对象中指定代理,则http模块将使用globalAgent,默认情况下将keepAlive设置为false。
因此,创建代理,并将其用于所有请求:
var http = require('http');
var agent = new http.Agent({ keepAlive: true }); // false by default
http.request({
host: 'api.development.push.apple.com',
port: 443,
path: '/3/device/1',
method: 'POST',
agent: agent, // use this agent for more requests as needed
}).end();https://stackoverflow.com/questions/38608038
复制相似问题