前言
nestjs项目,使用以下依赖
"axios": "^0.24.0",
"tunnel": "^0.0.6"
"@nestjs/cli": "^9.0.0",
正文
通过axios配置代理ip发送网络请求
npm install axios tunnel --save
import axios from 'axios';
const tunnel = require('tunnel');
async getList(item) {
let body = {
id: item.id,
order: 'asc',
page: 1,
};
// 配置代理ip
const tunnelProxy = tunnel.httpsOverHttp({
proxy: {
host: '127.0.0.1',
port: 8080,
}
});
await axios.post(this.url, body,{
proxy: false,
httpsAgent:tunnelProxy,
headers:{
token:this.config.token
},
// timeout:2000
}).then(async res => {
// console.log(JSON.stringify(res.config));
console.log('res',res)
}).catch(err => {
// 通过err.toJSON().message 可以拿到错误信息
console.log(`${getDate()} 错误信息:`, err.toJSON().message);
});
}
报错
tunneling socket could not be established, statusCode=400
tunneling socket could not be established, statusCode=407
tunneling socket could not be established, statusCode=503
tunneling socket could not be established, cause=connect ECONNREFUSED 61.154.46.114:38592
- 都是代理ip不可能,使用了我的代码代理是可以跑通的,报407就是没有配置白名单,权限不够,其他错误暂时不明,懂的指点指点~