Home > Web Front-end > JS Tutorial > body text

Create a proxy in Node.js

WBOY
Release: 2023-08-24 21:41:05
forward
1211 people have browsed it

在 Node.js 中创建代理

You can create an agent instance in Node.js using the new Agent() method. The http.request() method creates a custom http.Agent instance using the globalAgent from the "http" module.

Syntax

new Agent({options})
Copy after login

Parameters

The above function can accept the following parameters

  • options < /strong> – These options will contain configurable options that can be set on the agent at creation time. Following are the fields/options a proxy can have -

    • keepAlive - This method keeps the socket whether it has any outstanding requests or not, but retains them to For use by any future requests without actually re-establishing the TCP connection. This connection can be closed using Close Connection. Default value: false.

    • keepAliveMsecs - This option defines the initial delay for TCP keep-Alive packets when the keepAlive option is set to true. The default value is 1000.

    • maxSockets - This option defines the maximum number of sockets allowed per host. By default, this value is infinity.

    • maxTotalSockets – The total number of sockets allowed for all hosts. Each request uses a new socket until the limit is reached. The default value is infinity.

    • maxFreeSockets - This is the maximum number of free sockets that can be kept open in the idle state. The default value is: 256.

    • Scheduling - This is the scheduling policy that can be applied when selecting the next free socket to use. Schedule can be "fifo" or "lifo".

    • Timeout - Represents the socket timeout in milliseconds.

Example

Create a file called agent.js and copy the following code snippet. After creating the file, run this code using the following command as shown in the example below -

node agent.js
Copy after login

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);
Copy after login

Output

C:\homeode>> node agent.js
Succesfully created connection with agent: function toString() { [native code] }
Succesfully created connection with alive agent: function toString() { [native code] }
Copy after login

Example

The "agentkeepalive" module provides better flexibility when trying to create sockets or agents. We will use this module in the following examples for better understanding.

Installation

npm install agentkeepalive --save
Copy after login

Program code

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

Output

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; }
Copy after login

The above is the detailed content of Create a proxy in Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!