Hapi.js自定义404错误页面

时间:2015-01-20 02:55:42

标签: javascript node.js error-handling hapijs

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

希望处理程序像这样

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

2 个答案:

答案 0 :(得分:9)

您可以使用以下内容:

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

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

答案 1 :(得分:3)

小更新:自9.x版本起,您需要包含Inert插件才能使用reply.file。请参阅此问题:https://github.com/hapijs/hapi/issues/2729

(此时文档尚未更新)

相关问题