节点获取响应503,其中请求响应200后面的代理

时间:2017-12-07 21:52:23

标签: javascript node.js proxy request fetch

下面我有两个http请求,一个是request,另一个是isomorphic-fetchnode-fetch)。出于某种原因,request的请求有效,但node-fetch正在回复错误代码503。我在fetch版本中缺少什么?

const URL = require('url')
const fetch = require('isomorphic-fetch')
const HttpsProxyAgent = require('https-proxy-agent')
const request = require('request');

const url = process.env.URL
const proxy = process.env.PROXY

const requestPromise = function (url, options) {
    if (/^\/\//.test(url)) {
    url = 'https:' + url;
    }

    return new Promise(function(resolve, reject) {
        return request.call(this, url, options, function (err, res, body) {
            if (err) {
            throw new Error(err);
            }

            res.ok = true;
            res.json = function () {
            return JSON.parse(res.body);
            }
            return resolve(res);
        });
    });
};

function getProxy (url) {
    const parsedProxyURL = URL.parse(url);
    parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';
    return parsedProxyURL
}

requestPromise(url, {
    agent:new HttpsProxyAgent(getProxy(proxy))
})
    .then(console.log)
    .catch(console.log)


fetch(url, {
    agent:new HttpsProxyAgent(getProxy(proxy))
})
    .then(console.log)
    .catch(console.log)

1 个答案:

答案 0 :(得分:2)

request模块似乎是自动设置一个标头,host模块不是fetch。通过进行此更改,它可以正常工作。

const URL = require('url')
const fetch = require('isomorphic-fetch')
const HttpsProxyAgent = require('https-proxy-agent')
const request = require('request')

function getProxy (url) {
    const parsedProxyURL = URL.parse(url)
    parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:'
    return parsedProxyURL
}

const url = process.env.URL
const proxy = process.env.PROXY

fetch(url, {
    headers: {host: URL.parse(url).host},
    agent: new HttpsProxyAgent(getProxy(proxy))
})
    .then(res => res.json()).then(res => {
        console.log(res)
    })
    .catch(console.log)