在 Node.js 中建立 Agent


可以使用新的 Agent() 方法在 Node 中建立一個 Agent 例項。http.request() 方法使用 'http' 模組中的 globalAgent 來建立一個自定義的 http.Agent 例項。

語法

new Agent({options})

引數

上述函式可以接受以下 **引數** −

  • **options** – 這些選項包含在建立 Agent 時可以設定的可配置選項。以下是 Agent 可以具有的欄位/選項 −

    • **keepAlive** – 此方法無論是否有未完成的請求,都會保留套接字,但會將其保留以用於任何未來的請求,而無需實際重新建立 TCP 連線。可以使用 'close' 連線來關閉此連線。預設值:false。

    • **keepAliveMsecs** – 當將 keepAlive 選項設定為 true 時,此選項定義 TCP keep-Alive 資料包的初始延遲。預設值為 1000。

    • **maxSockets** – 此選項定義每個主機允許的最大套接字數。預設值為無窮大。

    • **maxTotalSockets** – 允許所有主機使用的套接字總數。每個請求都會使用新的套接字,直到達到限制為止。預設值為無窮大。

    • **maxFreeSockets** – 這是可以保持空閒狀態的最大空閒套接字數。預設值:256。

    • **scheduling** – 這是在選擇下一個要使用的空閒套接字時可以應用的排程策略。排程可以是 'fifo' 或 'lifo'。

    • **timeout** – 以毫秒為單位表示套接字超時。

示例

建立一個名為 agent.js 的檔案並複製下面的程式碼片段。建立檔案後,使用以下命令執行此程式碼,如下例所示:

node agent.js

agent.js

線上演示

// Node.js program to demonstrate the creation of new Agent

// Importing the http module
const http = require('http');

// Creating a new agent
var agent = new http.Agent({});

// Defining options for agent
const aliveAgent = new http.Agent({
   keepAlive: true, maxSockets: 5,
});

// Creating connection with alive agent
var aliveConnection = aliveAgent.createConnection;

// Creating new connection
var connection = agent.createConnection;

// Printing the connection
console.log('Succesfully created connection with agent: ',
connection.toString);
console.log('Succesfully created connection with alive agent: ',
aliveConnection.toString);

輸出

C:\home
ode>> node agent.js Succesfully created connection with agent: function toString() { [native code] } Succesfully created connection with alive agent: function toString() { [native code] }

示例

'agentkeepalive' 模組在嘗試建立套接字或 Agent 時提供了更好的靈活性。我們將在下面的示例中使用此模組以更好地理解。

安裝

npm install agentkeepalive --save

程式程式碼

// Node.js program to demonstrate the creation of new Agent

// Importing the http module
const http = require('http');
// Importing the agentkeepalive module
const Agent = require('agentkeepalive');

// Creating a new agent
const keepAliveAgent = new Agent({});

// Implementing some options
const options = {
   host: 'tutorialspoint.com',
   port: 80,
   path: '/',
   method: 'GET',
   agent: keepAliveAgent,
};

// Requesting details via http server module
const req = http.request(options, (res) => {
   // Printing statuscode, headers and other details
   // received from the request
   console.log("StatusCode: ", res.statusCode);
   console.log("Headers: ", res.headers);
});

// Printing the agent options
console.log("Agent Options: ", req.agent.options);
req.end();

輸出

C:\home
ode>> node agent.js Agent Options: { socketActiveTTL: 0,    timeout: 30000,    freeSocketTimeout: 15000,    keepAlive: true,    path: null } StatusCode: 403 Headers: { date: 'Sun, 25 Apr 2021 08:21:14 GMT',    server: 'Apache',    'x-frame-options': 'SAMEORIGIN',    'last-modified': 'Thu, 16 Oct 2014 13:20:58 GMT',    etag: '"1321-5058a1e728280"',    'accept-ranges': 'bytes',    'content-length': '4897',    'x-xss-protection': '1; mode=block',    vary: 'User-Agent',    'keep-alive': 'timeout=5, max=100',    connection: 'Keep-Alive',    'content-type': 'text/html; charset=UTF-8' }

更新於:2021年5月20日

907 次瀏覽

啟動您的 職業生涯

完成課程後獲得認證

開始
廣告