Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点获取以503响应,其中请求以200作为代理响应_Javascript_Node.js_Proxy_Request_Fetch - Fatal编程技术网

Javascript 节点获取以503响应,其中请求以200作为代理响应

Javascript 节点获取以503响应,其中请求以200作为代理响应,javascript,node.js,proxy,request,fetch,Javascript,Node.js,Proxy,Request,Fetch,下面我有两个http请求,一个是通过请求发出的,另一个是通过同构获取(节点获取)发出的。由于某种原因,request的请求可以工作,但是节点fetch返回错误代码503。取回版本中是否缺少任何内容 const URL = require('url') const fetch = require('isomorphic-fetch') const HttpsProxyAgent = require('https-proxy-agent') const request = require('requ

下面我有两个http请求,一个是通过
请求
发出的,另一个是通过
同构获取
节点获取
)发出的。由于某种原因,
request
的请求可以工作,但是
节点fetch
返回错误代码
503
。取回版本中是否缺少任何内容

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)

请求
模块似乎正在自动设置一个头,而
获取
模块的
主机
则没有。通过做出这种改变,它是有效的

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)

请求
模块似乎正在自动设置一个头,而
获取
模块的
主机
则没有。通过做出这种改变,它是有效的

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)

较新的Nodejs应该具有本机fetch,同构的是使用node fetch:这应该可以工作:您是在回答中还是在这里尝试将url作为字符串@HMR正如您在上面看到的,我所做的与您所展示的差不多,而不是将字符串传递给
HttpsProxyAgent
,您可以提供选项。
request
node fetch
之间仍然存在差异,这在
request
中是默认的,但在
node fetch
中不存在。较新的Nodejs应该具有本机fetch,同构的是使用node fetch:这应该有效:您是在回答中还是在这里尝试了作为字符串的url@HMR正如您在上面看到的,我所做的与您所展示的差不多,而不是将字符串传递给
HttpsProxyAgent
,您可以提供选项。
request
node fetch
之间仍然存在差异,后者在
request
中是默认的,使其工作,但在
node fetch
中不存在。