Node.jsのagent.createConnection()メソッド
agent.createConnection()メソッドは、「http」モジュールによって提供されるインターフェースです。このメソッドは、HTTPリクエストに使用できるソケット/ストリームを生成します。カスタムエージェントを使用してこのメソッドをオーバーライドし、柔軟性を高めることができます。ソケット/ストリームは、この関数から直接ソケット/ストリームを返すか、このソケット/ストリームをコールバックに渡すかの2つの方法で返すことができます。
構文
agent.createConnection(options, [callback])
パラメータ
上記の関数は、次のパラメータを受け入れることができます-
-
オプション –これらのオプションには、ストリームを作成する必要がある接続の詳細が含まれます。
-
コールバック –これにより、作成されたソケット接続がエージェントから受信されます。
例
-connection.jsという名前のファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すようにこのコードを実行します-
node connection.js
connection.js
// Node.js program to demonstrate the creation of socket
// using agent.createConnection() method
// Importing the http module
const http = require('http');
// Creating a new agent
var agent = new http.Agent({});
// Creating connection with the above agent
var conn = agent.createConnection;
console.log('Connection is succesfully created !');
// Printing connection details
console.log('Connection: ', conn); 出力
C:\home\node>> node connection.js
Connection is succesfully created !
Connection: function connect(...args) {
var normalized = normalizeArgs(args);
var options = normalized[0];
debug('createConnection', normalized);
var socket = new Socket(options);
if (options.timeout) {
socket.setTimeout(options.timeout);
}
return socket.connect(normalized);
} 例
もう1つの例を見てみましょう。
// Node.js program to demonstrate the creation of socket
// using agent.createConnection() method
// 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: 0, 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 connection.js
Succesfully created connection with agent: function toString() { [native code] }
Succesfully created connection with alive agent: function toString() { [native code] }> -
Node.jsのscript.createCachedData()メソッド
script.createCachedData()メソッドは、スクリプトコンストラクターのcachedDataオプションとともに使用されるコードキャッシュを作成するために使用されます。このcachedDataは、待ち時間なしで複数回呼び出すことができます。このメソッドは、「script」モジュールから組み込まれたプログラミングインターフェイスです。 構文 script.createCachedData() パラメータ データをキャッシュするだけなので。ユーザーからの特定の入力は必要ありません。キャッシュされたバッファのみを返します。 例 createCachedData.jsという名前のフ
-
Node.jsのprocess.env()メソッド
process.argv()メソッドは、ユーザー環境オブジェクトを取得するために使用されます。このオブジェクトには、このコマンドが実行されている環境に関するすべての情報が含まれます。 構文 process.env() パラメータ ユーザー環境のオブジェクトを返すためです。ユーザーからの入力は必要ありません。 例 env.jsという名前のファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すようにこのコードを実行します- node env.js env.js // Node.js program to demonstrat