我正在调用“ themoviedb.org”的REST API,当我通过办公室网络发出请求时,出现以下错误:
{"errno":"ETIMEDOUT","code":"ETIMEDOUT","syscall":"connect","address":"54.164.67.128","port":443}
当我通过家庭网络发出请求时,我得到了预期的结果。我知道我必须启用代理设置,但是我不知道该怎么做,或者我必须在系统或代码中进行设置。请帮忙。我的代码如下:
const apiKey = "----tmdb API KEY----";
const https = require("https");
const { URL }= require('url');
function getMovieByID(id, callback){
console.log("Inside getMovieByID ID: "+ id);
var options = new URL("https://api.themoviedb.org/3/movie/"+id+"?api_key="+apiKey);
var req = https.request(options, function (res) {
let chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
let body = Buffer.concat(chunks);
callback(null, body.toString());
});
});
req.on("error", function(err) {
callback(err, null);
});
req.end();
};
getMovieByID("75780", (err,data) =>{
var temp ="";
if (err != null){
console.log("Error:", JSON.stringify(err));
process.exit()
}else{
//console.log("Movies:\n",data);
temp = data
}
console.log("Result:\n", temp)
process.exit()
});
答案 0 :(得分:1)
尝试
var https = require('https');
var ProxyAgent = require('proxy-agent');
var url = "https://api.themoviedb.org/3/movie/"+id+"?api_key="+apiKey
// HTTP, HTTPS, or SOCKS proxy to use
var proxyUri = process.env.https_proxy || "https://" + user + ":" + password + "@" + host + ":" + port;;
var opts = {
agent: new ProxyAgent(proxyUri)
};
// the rest works just like any other normal HTTP request
https.get(url,opts,function(res){
});
注意:如果代理服务器不需要用户名和密码,则使用proxyUrl
对于http端点:http://example.com或http://ip:port
对于https端点https://example.com或https://ip:port