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

标签 node.js hapi

我正在使用 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 实例上我安装了 Node 版本 15.5.0这显然与最新版本的 hapi.js ( 20.0.2 ) 不兼容。
要解决此问题,只需安装 Node 版本 14.15.3 .

关于node.js - Hapi.js 无法读取 null 的属性 'statusCode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65485160/

相关文章:

node.js - 如何在 Node.js 中正确使用 D3?

javascript - 如何将 result.insertId 分配给我在 query() 函数中传递的函数中的 id?

node.js - postman 上的 POST 请求未完成

java - 使用 java 的 HL7 版本 2.7 解析器(Hapi 除外)

node.js - 如何检测RSA私钥在 Node 中有密码

mysql - 从 NodeJS 抛出错误中获取错误消息

javascript - 如何使用node.js将表单数据从客户端脚本发送到服务器脚本

java - hapi 库 'getAll(String name)' 不返回消息中的所有自定义 z 段

node.js - HEROKU 和 HAPIJS 错误 R10(启动超时)-> Web 进程在启动后 60 秒内无法绑定(bind)到 $PORT

java - 如何使用 ZPM 段解析 DFT_P03 消息