Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Tạo Agent trong Node.js

Mô tả

Bạn có thể dùng phương thức new Agent() để tạo một instance của agent trong Node. Phương thức http.request() sử dụng globalAgent từ module http để tạo một instance http.Agent tùy chỉnh.

Cú pháp

new Agent({options})

Tham số

Hàm trên có thể chấp nhận các Tham số sau:

  • options – Chứa các tùy chọn cấu hình có thể thiết lập trên Agent khi tạo. Dưới đây là các trường/tùy chọn Agent có thể có:

    • keepAlive – Giữ socket xung quanh dù có yêu cầu未完成 hay không, nhưng giữ chúng cho các yêu cầu tương lai mà không cần thiết lập lại kết nối TCP. Có thể dùng 'close connection' để đóng kết nối này. Giá trị mặc định: false.

    • keepAliveMsecs – Khi dùng keepAlive là true, tùy chọn này định nghĩa độ trễ ban đầu cho gói TCP keep-Alive. Giá trị mặc định là 1000.

    • maxSockets – Tùy chọn này định nghĩa số socket tối đa được phép cho mỗi host. Mặc định giá trị này là infinity.

    • maxTotalSockets – Tổng số socket được phép cho tất cả các host. Mỗi yêu cầu dùng một socket mới cho đến khi đạt giới hạn. Giá trị mặc định là Infinity.

    • maxFreeSockets – Số socket rảnh tối đa có thể để mở ở trạng thái rảnh. Giá trị mặc định: 256.

    • scheduling – Chiến lược lập lịch dùng khi chọn socket rảnh tiếp theo. Có thể là 'fifo' hoặc 'lifo'.

    • timeout – Thời gian chờ socket tính bằng mili giây.

Ví dụ

Tạo file tên agent.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:

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);

Đầu ra

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] }

Ví dụ khác

Module agentkeepalive cung cấp độ linh hoạt tốt hơn khi cố gắng tạo socket hoặc agent. Chúng ta sẽ dùng module này trong ví dụ dưới để hiểu rõ hơn.

Cài đặt

npm install agentkeepalive --save

Mã chương trình

// 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();

Đầu ra

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' }