Node.js new Agent() 方法
Node.js HTTP API是低级的,因此它可以支持 HTTP 应用程序。为了访问和使用 HTTP 服务器和客户端,我们需要调用它们(通过 ' require('http') ')。 HTTP 消息头以 JSON 格式表示。
新的 Agent({}) (在 v0.3.4 中添加)方法是“http”模块的内置应用程序编程接口(API),其中http.request()使用默认globalAgent ,它应该创建自定义http.Agent实例。
句法:
new Agent({options})
参数:此函数接受如上所述和如下所述的单个对象参数:
options < Object > 它是可以在代理上设置的可配置选项。
- keepAlive < boolean > :默认值设置为false。它仍然保留套接字是否有未完成的请求,因此它可以用于将来的请求而无需重新建立连接( TCP )。使用代理时会发送 keep-alive 标头连接,并使用“关闭”连接来关闭连接。
- keepAliveMsecs < number > :默认值设置为 false。它表示 TCP Keep-Alive 数据包的初始延迟,如果 keepAlive 选项为 false 或未定义,则被忽略。
- maxSockets < number > :默认值设置为 Infinity。它允许每个主机的最大套接字数,并且在达到最大值之前,每个请求都使用一个新的套接字。
- maxTotalSockets
:默认值设置为 Infinity。它允许所有主机的最大套接字总数,并且在达到最大值之前,每个请求都使用一个新的套接字。 - maxFreeSockets < number > :默认值设置为 256。为了在空闲状态下保持打开状态,它使用最大数量的套接字,并且仅当 keepAlive 设置为true时才相关。
- 调度<字符串> :默认调度是先进先出。这是一种选择下一个空闲套接字进行调度和使用的策略。它有两种类型“先进先出”或“后进先出”。 ' LIFO '(后进先出)选择最近使用的套接字,而'FIFO'(先进先出)选择最近最少使用的套接字。
- timeout < number > :它以毫秒为单位计算套接字超时,并在创建套接字时设置超时。
以下示例说明了在 Node.js 中使用new Agent({})方法。
示例 1:文件名:index.js
// Node.js program to demonstrate the
// new agent({}) method
// Importing http module
const http = require('http');
var agent = new http.Agent({});
// Creating new agent
const aliveAgent = new http.Agent({ keepAlive: true,
maxSockets: 0, maxSockets: 5, });
// Creating new agent
var agent = new http.Agent({});
// Creating new connection
var createConnection = aliveAgent.createConnection;
// Creating new connection
var createConnection = agent.createConnection;
console.log('Connection successfully created...');
// Printing the connection
console.log(createConnection);
console.log('Connection successfully created...');
// Printing the connection
console.log('Connection: ', createConnection);
使用以下命令运行index.js文件:
node index.js
输出:
Connection successfully created…
[Function: connect]
Connection successfully created…
Connection: [Function: connect]
另一个 Module agentkeepalive更适合与 Http 兼容,这使得处理请求更容易。为了使用“ agentkeepalive”模块,我们需要安装 NPM(节点包管理器)和以下(在 cmd 上)。
// Creates package.json file
>> npm init
// Installs express module
>> npm install agentkeepalive --save OR
>> npm i agentkeepalive -s
导入agentkeepalive模块:导入agentkeepalive模块并将返回的实例存储到变量中。
const Agent = require('agentkeepalive');
示例 2:文件名:index.js
// Node.js program to demonstrate the
// new agent({}) method
// Importing http module
const http = require('http');
// Importing agentkeepalive module
const Agent = require('agentkeepalive');
// Creating new agent
const keepAliveAgent = new Agent({});
// Options object
const options = {
host: 'geeksforgeeks.org',
port: 80,
path: '/',
method: 'GET',
agent: keepAliveAgent,
};
// Requesting via http server module
const req = http.request(options, (res) => {
// console.log(require('util').inspect(res, depth=0));
// Printing statuscode
console.log("StatusCode: ", res.statusCode);
// Printing headers
console.log("Headers: ", res.headers);
});
// Printing agent options
console.log("Agent Options: ", req.agent.options);
// console.log(req.agent.sockets);
req.end();
使用以下命令运行index.js文件:
node index.js
输出:
>> Agent Options: { keepAlive: true,
freeSocketTimeout: 15000,
timeout: 30000,
socketActiveTTL: 0,
path: null}
>> StatusCode: 301
>> Headers: { date: ‘Wed, 19 Aug 2020 11:19:23 GMT’,
server: ‘Apache’,
location: ‘https://www.geeksforgeeks.org/’,
‘content-length’: ‘238’,
‘keep-alive’: ‘timeout=5, max=100’,
connection: ‘Keep-Alive’,
‘content-type’: ‘text/html; charset=iso-8859-1’}
参考: https://nodejs.org/api/http.html#http_new_agent_options