Hapi.js自定义404错误页面

时间:2022-10-09 20:58:08

Very new to Hapi framework. I am trying to make custom error pages. How do you route a 404.html page to 404 response?

Hapi框架非常新。我正在尝试制作自定义错误页面。如何将404.html页面路由到404响应?

would like the handler to be like so

希望处理程序是这样的

handler: function (request, reply) {
    reply.file('./static/website/javascript/main.js');
}

2 个解决方案

#1


9  

You could use something like this:

你可以使用这样的东西:

server.route({
    method: '*',
    path: '/{p*}', // catch-all path
    handler: function (request, reply) {
        reply.file('./path/to/404.html').code(404);
    }
});

By using the .code() method, you can override the default (200 OK) status code.

通过使用.code()方法,您可以覆盖默认(200 OK)状态代码。

#2


3  

A small update: since 9.x version you need to include Inert plugin to use reply.file. See this issue: https://github.com/hapijs/hapi/issues/2729

一个小的更新:从9.x版本开始,你需要包含Inert插件才能使用reply.file。请参阅此问题:https://github.com/hapijs/hapi/issues/2729

(By this time documentation is not updated yet)

(此时文档尚未更新)

#1


9  

You could use something like this:

你可以使用这样的东西:

server.route({
    method: '*',
    path: '/{p*}', // catch-all path
    handler: function (request, reply) {
        reply.file('./path/to/404.html').code(404);
    }
});

By using the .code() method, you can override the default (200 OK) status code.

通过使用.code()方法,您可以覆盖默认(200 OK)状态代码。

#2


3  

A small update: since 9.x version you need to include Inert plugin to use reply.file. See this issue: https://github.com/hapijs/hapi/issues/2729

一个小的更新:从9.x版本开始,你需要包含Inert插件才能使用reply.file。请参阅此问题:https://github.com/hapijs/hapi/issues/2729

(By this time documentation is not updated yet)

(此时文档尚未更新)