【问题标题】:hapi returning 500 error even when axios return 201即使 axios 返回 201,hapi 也会返回 500 错误
【发布时间】:2020-03-20 09:41:49
【问题描述】:

我正在使用带有 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;
})

任何关于我可能做错了什么的见解?

【问题讨论】:

  • 从 axios 调用中得到响应后,您是否正在做一些响应?
  • 我不是。和上面的代码 sn-p 一样。

标签: axios hapijs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 2021-03-24
    • 2020-09-24
    • 2020-08-09
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多