首頁 > web前端 > js教程 > 主體

在 Node.js 中建立代理

WBOY
發布: 2023-08-24 21:41:05
轉載
1211 人瀏覽過

在 Node.js 中创建代理

您可以使用 new Agent() 方法在 Node.js 中建立代理實例。 http.request() 方法使用「http」模組中的 globalAgent 建立自訂 http.Agent 實例。

語法

new Agent({options})
登入後複製

參數

上述函數可以接受以下參數

  • 選項< /strong> – 這些選項將包含建立時可以在代理程式上設定的可設定選項。以下是代理可以擁有的字段/選項-

    • keepAlive  - 此方法保持套接字是否有任何未完成的請求或不,但保留它們以供將來的任何請求使用,而無需實際重新建立TCP 連線。可以使用「關閉連線」來關閉此連線。預設值: false。

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

    • maxSockets - 此選項定義每個主機允許的最大套接字數。預設情況下,該值為無窮大。

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

    • maxFreeSockets - 這是在空閒狀態下可以保持開啟狀態的空閒套接字的最大數量。預設值為:256。

    • 調度 - 這是在選擇下一個要使用的空閒套接字時可以套用的調度策略。調度可以是“fifo”或“lifo”。

    • 逾時 - 表示套接字逾時(以毫秒為單位)。

範例

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

node agent.js
登入後複製

agent.js

 現場示範

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

// Importing the http module
const http = require(&#39;http&#39;);

// 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(&#39;Succesfully created connection with agent: &#39;,
connection.toString);
console.log(&#39;Succesfully created connection with alive agent: &#39;,
aliveConnection.toString);
登入後複製

輸出

C:\homeode>> node agent.js
Succesfully created connection with agent: function toString() { [native code] }
Succesfully created connection with alive agent: function toString() { [native code] }
登入後複製

範例

「agentkeepalive」模組在嘗試建立套接字或代理程式時提供了更好的靈活性。我們將在下面的範例中使用此模組以便更好地理解。

安裝

npm install agentkeepalive --save
登入後複製

程式碼

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

// Importing the http module
const http = require(&#39;http&#39;);
// Importing the agentkeepalive module
const Agent = require(&#39;agentkeepalive&#39;);

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

// Implementing some options
const options = {
   host: &#39;tutorialspoint.com&#39;,
   port: 80,
   path: &#39;/&#39;,
   method: &#39;GET&#39;,
   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:\homeode>> node agent.js
Agent Options: { socketActiveTTL: 0,
   timeout: 30000,
   freeSocketTimeout: 15000,
   keepAlive: true,
   path: null }
StatusCode: 403
Headers: { date: &#39;Sun, 25 Apr 2021 08:21:14 GMT&#39;,
   server: &#39;Apache&#39;,
   &#39;x-frame-options&#39;: &#39;SAMEORIGIN&#39;,
   &#39;last-modified&#39;: &#39;Thu, 16 Oct 2014 13:20:58 GMT&#39;,
   etag: &#39;"1321-5058a1e728280"&#39;,
   &#39;accept-ranges&#39;: &#39;bytes&#39;,
   &#39;content-length&#39;: &#39;4897&#39;,
   &#39;x-xss-protection&#39;: &#39;1; mode=block&#39;,
   vary: &#39;User-Agent&#39;,
   &#39;keep-alive&#39;: &#39;timeout=5, max=100&#39;,
   connection: &#39;Keep-Alive&#39;,
   &#39;content-type&#39;: &#39;text/html; charset=UTF-8&#39; }
登入後複製
#

以上是在 Node.js 中建立代理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!