首页 > 解决方案 > 将站点部署到 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

标签: javascriptnode.jsexpressheroku

解决方案


推荐阅读