Node.js HTTP API是低級的,因此它可以支持HTTP應用程序。為了訪問和使用HTTP服務器和客戶端,我們需要調用它們(通過“ require(‘http’)”)。 HTTP消息頭表示為JSON格式。
agent.maxSockets(在v0.3.6中添加)方法是Http模塊的內置應用程序編程接口,該接口確定代理每個源可以打開多少個並發套接字。源是agent.getName()的返回值。
為了獲得響應和正確的結果,我們需要導入‘http’模塊。
導入:
const http = require('http');
用法:
agent.maxSockets;
參數:如上所述,該函數不接受任何參數。
返回值<number>:默認情況下,它設置為Infinity。它確定每個原始代理可以打開多少個並發套接字。
以下示例說明了Node.js中agent.maxSockets方法的用法。
範例1: 文件名:index.js
// Node.js program to demonstrate the
// agent.maxSockets method
// Importing http module
const http = require('http');
// Importing agentkeepalive module
const Agent = require('agentkeepalive');
// Creating new agent
const keepAliveAgent = new Agent({});
console.log(keepAliveAgent.maxSockets);
// 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) => {
// Printing statuscode
console.log("StatusCode:", res.statusCode);
});
req.end();
使用以下命令運行index.js文件:
node index.js
Output:
Infinity
StatusCode: 301
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js agent.maxSockets Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。