Node.jsでのエージェントの作成
new Agent()メソッドを使用して、Nodeにエージェントのインスタンスを作成できます。 http.request()メソッドは、「http」モジュールのglobalAgentを使用して、カスタムhttp.Agentインスタンスを作成します。
構文
new Agent({options}) パラメータ
上記の関数は、次のパラメータを受け入れることができます −
-
オプション –これらのオプションには、作成中にエージェントに設定できる構成可能なオプションが含まれます。以下は、エージェントが持つことができるフィールド/オプションです-
-
キープアライブ –このメソッドは、未処理の要求があるかどうかに関係なくソケットを保持しますが、実際にTCP接続を再確立することなく、将来の要求のためにソケットを保持します。 'close' connection'を使用して、この接続を閉じることができます。デフォルト値:false。
-
keepAliveMsecs –キープアライブオプションをtrueとして使用すると、このオプションはTCPキープアライブパケットの初期遅延を定義します。デフォルト値は1000です。
-
maxSockets –このオプションは、ホストごとに許可されるソケットの最大数を定義します。デフォルトでは、この値は無限大です。
-
maxTotalSockets –すべてのホストに許可されているソケットの総数。制限に達するまで、各リクエストは新しいソケットを使用します。デフォルト値はInfinityです。
-
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('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\node>> 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('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\node>> 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' } -
Javascriptでのプリムのアルゴリズム
Primのアルゴリズムは、重み付き無向グラフの最小スパニングツリーを見つける欲張りアルゴリズムです。すべての頂点を含むツリーを形成するエッジのサブセットを検出し、ツリー内のすべてのエッジの合計の重みが最小化されます。 アルゴリズムは、ツリーから別の頂点への可能な限り安価な接続を追加する各ステップで、任意の開始頂点から一度に1つの頂点でこのツリーを構築することによって動作します。 プリムのアルゴリズムはどのように機能しますか? プリムのアルゴリズムがどのように機能するかを示す図を見てみましょう- 1.ルートノードとして任意のノードを選択します。この場合、Primのスパニングツリーのルートノ
-
Node.jsにExpress-rate-limitを統合する
WebサイトがDOSおよびDDOS攻撃を受けないようにするために、レート制限は日々重要になっています。レート制限は、システムがあらゆる種類の偽の要求やその他のブルートフォース攻撃から保護します。レート制限は、IPが要求を行うことができる回数を制限します。 expressrate-limitは、ユーザーからのリクエスト数を制限するためのnpmパッケージです。 レート制限モジュールのインストール 以下のコマンドを実行して、エクスプレスレート制限モジュールをアプリケーションにインストールします。 npm install --save express-rate-limit 例 名前がrateLim