javascript - 将站点部署到 Heroku 后,无法连接到外部 API,但可以在本地工作(Node.js)
问题描述
我的网站对外部 API 执行“GET”请求以检索数据。它在本地工作。但是,在将我的站点部署到 Heroku 后,我无法再连接到外部 API 来检索数据。我收到 503 服务不可用错误。
我目前正在为 http-request 使用“GOT”npm 模块,并且我尝试了几个不同的模块,它们都可以在本地工作,但不能通过 heroku。我认为这可能是标题问题,但我无法确定可能出了什么问题。有人可以指导我解决问题吗?谢谢。
我为发出 GET 请求而创建的服务器端函数
var fetchApiData = function(targetSeason, targetCategory, callback) {
let options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
'Accept-Encoding': '*',
'Accept-Language': ('en'),
'origin': ('https://apidomain.nba.com')
}
};
(async () => {
try {
const response = await got(('https://apidomain.nba.com/stats/leagueleaders/?LeagueID=00&PerMode=PerGame&StatCategory=' + targetCategory + '&Season=' + targetSeason + '&SeasonType=Regular%20Season&Scope=S', options);
callback(null, JSON.parse(response.body));
} catch (error) {
callback(error.response.body, null);
}
})();
};
浏览器错误
POST https://myserver.herokuapp.com/player-data 503 (Service Unavailable)
Axios Error....... Error: Request failed with status code 503
at createError (createError.js?f777:16)
at settle (settle.js?199f:18)
at XMLHttpRequest.handleLoad (xhr.js?14ed:77)
Heroku 日志中的错误
2018-05-09T22:14:35.153304+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/player-data" host=myserver.herokuapp.com request_id=18a37de9-9f85-4c03-a038-bf0f90223e1e fwd="64.134.226.170" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https
解决方案
推荐阅读
- arrays - 如何创建一个存储素数的数组?
- javascript - 为什么在函数之外创建节点时只有最后一个附加有效?
- html - 为什么媒体打印中横向页面的内容隐藏
- python - django forloop 计数器作为变量
- python - 如何获取url列表并在scrapy python中用于Web数据提取
- javafx - JavaFX 绑定布局属性不适用于动态添加的节点
- postgresql - PGPOOL 从节点处于“等待”状态
- google-apps-script - 使用 getSheetByName() 和 ui.prompt() 中插入的 var
- c# - 代表两个参与者互相比赛的足球队的正则表达式
- ruby-on-rails - active admin 首字母大写