Hapi.js 无法读取 属性 'statusCode' 的 null

Hapi.js Cannot read property 'statusCode' of null

我正在使用 hapi.js 和 mongodb 创建一个 node.js api 服务器,我在 Amazon EC2 上运行时遇到了一些问题。 运行 它在本地工作,但如果我 运行 它在 EC2 实例上,我收到错误 TypeError: Cannot read property 'statusCode' of null

完整的堆栈跟踪如下:

TypeError: Cannot read property 'statusCode' of null
  at Request._finalize (/home/ec2-user/backend/node_modules/@hapi/hapi/lib/request.js:497:31)
  at Request._reply (/home/ec2-user/backend/node_modules/@hapi/hapi/lib/request.js:434:18)
  at Request._execute (/home/ec2-user/backend/node_modules/@hapi/hapi/lib/request.js:280:14)
  at processTicksAndRejections (node:internal/process/task_queues:93:5)

奇怪的是 GET 请求在 PUT、POST 和 DELETE 抛出上述错误时有效。 我已将 server.js 设置如下:

...
const init = async () => {

    const server = Hapi.server({
        port: 3000,
    });

    //server.route(routes);

    server.route([
      {
        method: "GET",
        path: "/test",
        handler: async (request, h) => {
          return "workin GET";
        },
      },
      {
        method: "PUT",
        path: "/test",
        handler: async (request, h) => {
          return "workin PUT";
        },
      },
      {
        method: "POST",
        path: "/test",
        handler: async (request, h) => {
          return "workin POST";
        },
      },
      {
        method: "DELETE",
        path: "/test",
        handler: async (request, h) => {
          return "workin DELETE";
        },
      },
    ]);

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

init();

有什么解决办法吗?

我发现我在 EC2 实例上安装了节点版本 15.5.0,这显然与最新版本的 hapi.js (20.0.2) 不兼容。

要解决此问题,只需安装节点版本 14.15.3

只需删除@hapi/hapi 并重新安装它