【问题标题】:How can i set timeout to axios that uses proxy option?如何为使用代理选项的 axios 设置超时?
【发布时间】:2020-07-16 15:59:14
【问题描述】:

我正在使用带有代理选项的 Axios 我想检查错误的代理,所以我决定为我的 GET 请求设置超时。

代码如下:

let res= await axios.get(`http://somedomain.com`,
        {
            timeout:1500,
            proxy: {
                
                host: proxyList[indexOfProxy].host,
                port: proxyList[indexOfProxy].port,
                auth: {
                    username: '',
                    password: ''
                },
                
            }
        }
    ).catch(err => {
        console.log(`proxy ${indexOfProxy} not working.`);
        
    });

但实际上 timeout 并没有起作用,它花了很长时间才去 catch 块。

【问题讨论】:

    标签: javascript node.js axios httprequest


    【解决方案1】:

    使用 axios.create()

    const axios = require('axios').default;
    
    const instance = axios.create({
        baseURL: 'https://wainot.trade',
        timeout: 5000,
      });
    
    async function run() {
        try {
            let res = await instance.get()
        } catch (error) {
            console.log('no')
        }
    }
    run()
    

    【讨论】:

    • 你在使用 try-catch 吗?
    猜你喜欢
    • 1970-01-01
    • 2020-01-19
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多