在 CentOS 上给 Node.js 配置代理,通常有几种场景,下面按常见用法给你说明:
如果你是在 CentOS 上用 npm 安装包,需要走代理:
npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890
npm config set proxy http://username:password@127.0.0.1:7890
npm config set https-proxy http://username:password@127.0.0.1:7890
npm config get proxy
npm config get https-proxy
npm config delete proxy
npm config delete https-proxy
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
node app.js
永久生效(写入环境变量):
echo 'export HTTP_PROXY=http://127.0.0.1:7890' >> ~/.bashrc
echo 'export HTTPS_PROXY=http://127.0.0.1:7890' >> ~/.bashrc
source ~/.bashrc
const axios = require('axios');
const httpsProxyAgent = require('https-proxy-agent');
const agent = new httpsProxyAgent('http://127.0.0.1:7890');
axios.get('https://api.github.com', { httpsAgent: agent })
.then(res => console.log(res.data));
const http = require('http');
const url = require('url');
const options = {
host: '127.0.0.1',
port: 7890,
method: 'CONNECT',
path: 'api.github.com:443'
};
yarn config set proxy http://127.0.0.1:7890
yarn config set https-proxy http://127.0.0.1:7890
vi /etc/profile
添加:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
source /etc/profile
✅ 代理不生效?
✅ 公司内网证书问题
npm config set strict-ssl false
如果你能说明:
我可以给你更精确的配置方案。