Node.jsでプロキシを作成する

WBOY
リリース: 2023-08-24 21:41:05
転載
1209 人が閲覧しました

在 Node.js 中创建代理

新しい Agent() メソッドを使用して、Node.js でエージェント インスタンスを作成できます。 http.request() メソッドは、「http」モジュールの globalAgent を使用してカスタム http.Agent インスタンスを作成します。

構文

new Agent({options})
ログイン後にコピー

パラメータ

上記の関数は、次の パラメータ-

  • を受け入れることができます。 #options < /strong> – これらのオプションには、作成時にエージェントに設定できる構成可能なオプションが含まれます。プロキシが持つことができるフィールド/オプションは次のとおりです -

    • keepAlive - このメソッドは、未処理のリクエストがあるかどうかに関係なくソケットを保持しますが、それらを For に保持します。実際に TCP 接続を再確立することなく、今後のリクエストで使用されます。この接続は、「接続を閉じる」を使用して閉じることができます。デフォルト値: false。

    • keepAliveMsecs - このオプションは、keepAlive オプションが true に設定されている場合の、TCP キープアライブ パケットの初期遅延を定義します。デフォルト値は 1000 です。

    • maxSockets - このオプションは、ホストごとに許可されるソケットの最大数を定義します。デフォルトでは、この値は無限大です。

    • maxTotalSockets – すべてのホストに許可されるソケットの合計数。各リクエストは、制限に達するまで新しいソケットを使用します。デフォルト値は無限大です。

    • maxFreeSockets - これは、アイドル状態で開いたままにできる空きソケットの最大数です。デフォルト値は 256 です。

    • スケジューリング - これは、次に使用する空きソケットを選択するときに適用できるスケジューリング ポリシーです。スケジュールには「fifo」または「lifo」を指定できます。

    • Timeout - ソケットのタイムアウトをミリ秒単位で表します。

agent.js というファイルを作成し、次のコード スニペットをコピーします。ファイルを作成した後、次の例に示すように、次のコマンドを使用してこのコードを実行します。 -

node agent.js
ログイン後にコピー

agent.js

Live Demo

// 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] }
ログイン後にコピー

Example

「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 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!