Node.js代理设置

Node.js代理设置

同样的代码在家里能运行,在公司就各种timeout

后来发现是因为公司是试用的代理网络。查了下npm都可以使用代理,但是node却没有设置代理的地方。

想问下大家是否知道如何给node设置一个全局代理,或者有没有什么模块有类似功能。

5 回复

Node.js 代理设置

在某些情况下,比如当你在公司网络中工作时,可能会遇到需要通过代理服务器才能访问外部资源的情况。这种情况下,你可能会发现你的Node.js应用在公司网络中运行时会遇到超时问题(timeout),而在家里的网络环境中却可以正常运行。

如何为Node.js设置全局代理

虽然Node.js本身并没有直接提供设置全局代理的方法,但你可以通过配置环境变量来实现这一点。具体来说,你可以设置 HTTP_PROXYHTTPS_PROXY 环境变量来指定代理服务器。

示例代码:

export HTTP_PROXY=http://your-proxy-server:port
export HTTPS_PROXY=http://your-proxy-server:port

如果你使用的是 Windows 系统,可以这样设置:

set HTTP_PROXY=http://your-proxy-server:port
set HTTPS_PROXY=http://your-proxy-server:port

使用 global-tunnel-ng 模块

如果上述方法不能满足你的需求,或者你希望在特定的Node.js项目中设置代理,可以考虑使用 global-tunnel-ng 模块。这个模块可以在全局范围内启用代理,适用于所有通过 http(s) 模块发起的请求。

安装 global-tunnel-ng

npm install global-tunnel-ng --save

使用示例:

const globalTunnel = require('global-tunnel-ng');
const http = require('http');

// 设置代理地址
globalTunnel.init('http://your-proxy-server:port', {
    username: 'your-username',
    password: 'your-password'
});

http.get('http://example.com', (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });
});

在这个示例中,我们首先导入了 global-tunnel-ng 模块,并调用了 init 方法来初始化代理服务器。然后,我们创建了一个简单的HTTP GET请求来验证代理是否生效。

通过以上方法,你应该能够在Node.js应用中正确设置代理,从而解决在公司网络环境下遇到的超时问题。


设置代理网络,跳转到nodejs服务器监听的ip:port

我用的是第三方的库,从第三方库发起http请求,不知道怎么设置。

直接用nodejs的api 在http的第一個參數option可以設置代理

对于 Node.js 来说,直接设置全局代理并不是一项内置的功能。不过你可以通过一些技巧或第三方模块来实现这一需求。

使用环境变量设置代理

最简单的方法是通过设置环境变量来配置 HTTP/HTTPS 代理。这种方法可以影响到所有依赖于这些环境变量的 Node.js 模块(如 http(s).requestaxios 等库)。

示例:

  1. 在命令行中临时设置

    export HTTP_PROXY=http://your-proxy-server:port
    export HTTPS_PROXY=http://your-proxy-server:port
    node your-script.js
    

    对于 Windows 用户,可以用以下命令:

    set HTTP_PROXY=http://your-proxy-server:port
    set HTTPS_PROXY=http://your-proxy-server:port
    node your-script.js
    
  2. 在代码中动态设置

    如果你想在 Node.js 脚本中动态设置代理,可以使用环境变量:

    process.env.HTTP_PROXY = 'http://your-proxy-server:port';
    process.env.HTTPS_PROXY = 'http://your-proxy-server:port';
    
    // 现在你可以在你的请求中使用 http(s).request
    const https = require('https');
    const request = https.request({
        hostname: 'example.com',
        port: 443,
        path: '/',
        method: 'GET'
    }, (res) => {
        console.log(`STATUS: ${res.statusCode}`);
        console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
        res.setEncoding('utf8');
        res.on('data', (chunk) => {
            console.log(`BODY: ${chunk}`);
        });
    });
    
    request.end();
    

使用第三方模块

如果你想要更方便地处理代理设置,可以使用一些现成的模块,例如 proxy-agentglobal-tunnel-ng

示例:

  1. 使用 proxy-agent

    const { request } = require('undici');
    const { ProxyAgent } = require('proxy-agent');
    
    const proxyUrl = 'http://your-proxy-server:port';
    const agent = new ProxyAgent(proxyUrl);
    
    request('https://example.com', {
      agent,
    })
    .then(response => {
      console.log(response.body);
    });
    
  2. 使用 global-tunnel-ng

    const globalTunnel = require('global-tunnel-ng');
    const axios = require('axios');
    
    globalTunnel.initialize('http://your-proxy-server:port', {
      port: 80,  // 可选,默认为80
    });
    
    axios.get('https://example.com')
    .then(response => {
      console.log(response.data);
    });
    

以上就是解决 Node.js 在代理环境下运行问题的几种方法,希望对你有所帮助。

回到顶部