即使axios返回201,hapi也将返回500错误

时间:2019-11-25 06:04:08

标签: axios hapijs

我正在使用带有hapi服务器的axios调用外部API。调试时,我看到axios get调用以标准axios形式返回状态为201的响应。虽然当我的hapi路线返回到客户端(浏览器)时,它会出现500错误。我的axios响应看起来像这样:

{
  data: '',
  status: 201,
  statusText: '',
  headers: {
   date: 'Mon, 25 Nov 2019 05:53:45 GMT',
   location: 'my-external-app-url',
   server: 'my-server-type',
   content-length: '0',
   connection: 'close'
  },
  config: {...},
  request: {...}
}

我的哈皮路线

server.route({ method: 'GET', path: '/status', handler: (req, h) => {
  const response = axios.get(...);

  return response;
})

对我可能做错的事情有何见解?

1 个答案:

答案 0 :(得分:0)

API调用应该是异步的。 使用异步/等待,您的代码将如下所示:

server.route({ method: 'GET', path: '/status', handler: async(req, h) => {
  const response = await axios.get(...);

  return response;
})