首页 > web前端 > js教程 > 正文

在 Node.js 中创建代理

WBOY
发布: 2023-08-24 21:41:05
转载
1304人浏览过

在 node.js 中创建代理

您可以使用 new Agent() 方法在 Node.js 中创建代理实例。 http.request() 方法使用“http”模块中的 globalAgent 创建自定义 http.Agent 实例。

语法

new Agent({options})
登录后复制

参数

上述函数可以接受以下参数

  • 选项 – 这些选项将包含创建时可以在代理上设置的可配置选项。以下是代理可以拥有的字段/选项 -

    • keepAlive  - 此方法保持套接字是否有任何未完成的请求或不,但保留它们以供将来的任何请求使用,而无需实际重新建立 TCP 连接。可以使用“关闭连接”来关闭此连接。默认值: false。

    • keepAliveMsecs - 将 keepAlive 选项设置为 true 时,此选项定义 TCP keep-Alive 数据包的初始延迟。默认值为 1000。

    • ma​​xSockets - 此选项定义每个主机允许的最大套接字数。默认情况下,该值为无穷大。

    • ma​​xTotalSockets – 所有主机允许的套接字总数。每个请求都使用一个新的套接字,直到达到限制。默认值为无穷大。

    • ma​​xFreeSockets - 这是在空闲状态下可以保持打开状态的空闲套接字的最大数量。默认值为: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:\homeode>> 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:\homeode>> 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' }
登录后复制

以上就是在 Node.js 中创建代理的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号