new Agent() 메서드를 사용하여 Node.js에서 에이전트 인스턴스를 생성할 수 있습니다. http.request() 메소드는 'http' 모듈의 globalAgent를 사용하여 사용자 정의 http.Agent 인스턴스를 생성합니다.
구문
new Agent({options}) 매개변수
위의 함수는 다음 매개변수 를 허용할 수 있습니다. -
-
옵션 – 이러한 옵션에는 생성하는 동안 에이전트에 설정할 수 있는 구성 가능한 옵션이 포함됩니다. 다음은 상담원이 가질 수 있는 필드/옵션입니다. -
-
keepAlive – 이 방법은 미해결 요청이 있는지 여부에 관계없이 소켓을 유지하지만 실제로 TCP 연결을 다시 설정하지 않고 향후 요청에 대해 소켓을 유지합니다. '닫기' 연결을 사용하여 이 연결을 닫을 수 있습니다. 기본값:false.
-
keepAliveMsecs – keepAlive 옵션을 true로 사용할 때 이 옵션은 TCP keep-Alive 패킷의 초기 지연을 정의합니다. 기본값은 1000입니다.
-
maxSockets – 이 옵션은 호스트당 허용되는 최대 소켓 수를 정의합니다. 기본적으로 이 값은 무한대입니다.
-
maxTotalSockets – 모든 호스트에 허용되는 총 소켓 수입니다. 각 요청은 제한에 도달할 때까지 새 소켓을 사용합니다. 기본값은 무한대입니다.
-
maxFreeSockets - 자유 상태에서 열린 상태로 둘 수 있는 최대 자유 소켓 수입니다. 기본값은 256입니다.
-
예약 – 다음으로 사용할 여유 소켓을 선택할 때 적용할 수 있는 스케줄링 전략입니다. 스케줄링은 'fifo' 또는 'lifo'일 수 있습니다.
-
시간 초과 – 소켓 시간 초과를 밀리초 단위로 나타냅니다.
-
예시
이름이 agent.js인 파일을 만들고 아래 코드 조각을 복사합니다. 파일을 생성한 후 다음 명령을 사용하여 아래 예와 같이 이 코드를 실행하십시오 -
node agent.js
에이전트.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' }