[英]In Hapi.js can I redirect to a different endpoint and set a statusCode?
[英]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)
奇怪的是,當 PUT、POST 和 DELETE 拋出上述錯誤時,GET 請求正在工作。 我已將 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 v20.2.1 中修復: https://github.com/hapijs/hapi/issues/4319 。
只需刪除 @hapi/hapi 並重新安裝即可
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.