Javascript 节点获取以503响应,其中请求以200作为代理响应
下面我有两个http请求,一个是通过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
请求
发出的,另一个是通过同构获取
(节点获取
)发出的。由于某种原因,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
中不存在。