Node.js HTTP API是低級的,因此它可以支持HTTP應用程序。為了訪問和使用HTTP服務器和客戶端,我們需要調用它們(通過“ require(‘http’)”)。 HTTP消息頭表示為JSON格式。
agent.maxFreeSockets(在v0.11.7中添加)方法是Http模塊的內置應用程序編程接口,用於設置在空閑狀態下保持打開狀態的最大套接字數。
為了獲得響應和正確的結果,我們需要導入‘http’模塊。
導入:
const http = require('http');
用法:
agent.maxFreeSockets;
參數:如上所述,該函數不接受任何參數。
返回值<number>:默認情況下,它設置為256。對於啟用了keepAlive的代理,這將設置在空閑狀態下保持打開狀態的最大套接字數。
以下示例說明了Node.js中agent.maxFreeSockets方法的使用。
示例1:Filename:index.js
// Node.js program to demonstrate the
// agent.maxFreeSockets 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.maxFreeSockets);
// 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
輸出:
256
StatusCode: 301
參考:https://nodejs.org/api/http.html#http_agent_maxfreesockets
相關用法
注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js agent.maxFreeSockets Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。