ubuntu

如何在Ubuntu上配置Node.js代理

小樊
50
2025-06-17 12:49:02
栏目: 编程语言

在Ubuntu上配置Node.js代理可以帮助你在开发和生产环境中更好地管理网络请求。以下是一些常见的方法来配置Node.js代理:

方法一:使用环境变量

  1. 设置HTTP代理

    export HTTP_PROXY=http://your-proxy-url:port
    export HTTPS_PROXY=https://your-proxy-url:port
    
  2. 设置NO_PROXY 如果你需要排除某些域名或IP地址不通过代理,可以设置NO_PROXY环境变量:

    export NO_PROXY=localhost,127.0.0.1,.example.com
    
  3. 在Node.js应用中使用 你可以在Node.js应用中读取这些环境变量:

    const http = require('http');
    
    const options = {
      hostname: 'example.com',
      port: 80,
      path: '/',
      method: 'GET',
      headers: {
        'Proxy-Authorization': 'Basic ' + Buffer.from('username:password').toString('base64')
      }
    };
    
    const req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
    });
    
    req.on('error', (e) => {
      console.error(`problem with request: ${e.message}`);
    });
    
    req.end();
    

方法二:使用http-proxy-agent模块

  1. 安装http-proxy-agent模块

    npm install http-proxy-agent
    
  2. 在Node.js应用中使用

    const http = require('http');
    const HttpProxyAgent = require('http-proxy-agent');
    
    const proxyAgent = new HttpProxyAgent({
      host: 'your-proxy-url',
      port: 'port'
    });
    
    const options = {
      hostname: 'example.com',
      port: 80,
      path: '/',
      method: 'GET',
      agent: proxyAgent
    };
    
    const req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
    });
    
    req.on('error', (e) => {
      console.error(`problem with request: ${e.message}`);
    });
    
    req.end();
    

方法三:使用https-proxy-agent模块

如果你需要代理HTTPS请求,可以使用https-proxy-agent模块:

  1. 安装https-proxy-agent模块

    npm install https-proxy-agent
    
  2. 在Node.js应用中使用

    const https = require('https');
    const HttpsProxyAgent = require('https-proxy-agent');
    
    const proxyAgent = new HttpsProxyAgent({
      host: 'your-proxy-url',
      port: 'port'
    });
    
    const options = {
      hostname: 'example.com',
      port: 443,
      path: '/',
      method: 'GET',
      agent: proxyAgent
    };
    
    const req = https.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
    });
    
    req.on('error', (e) => {
      console.error(`problem with request: ${e.message}`);
    });
    
    req.end();
    

方法四:使用noproxy配置

如果你使用的是Node.js的HTTP/HTTPS模块,并且想要在代码中直接配置代理,可以使用noproxy选项:

const http = require('http');
const HttpsProxyAgent = require('https-proxy-agent');

const proxyAgent = new HttpsProxyAgent({
  host: 'your-proxy-url',
  port: 'port'
});

const options = {
  hostname: 'example.com',
  port: 443,
  path: '/',
  method: 'GET',
  agent: proxyAgent,
  noproxy: 'localhost,127.0.0.1,.example.com'
};

const req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
});

req.on('error', (e) => {
  console.error(`problem with request: ${e.message}`);
});

req.end();

通过以上方法,你可以在Ubuntu上配置Node.js代理,以便更好地管理网络请求。选择适合你需求的方法进行配置即可。

0
看了该问题的人还看了