Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hapi.js custom 404 error page

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

would like the handler to be like so

handler: function (request, reply) {
    reply.file('./static/website/javascript/main.js');
}
like image 726
mdmullendore Avatar asked Jan 20 '15 02:01

mdmullendore


2 Answers

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.

like image 160
Matt Harrison Avatar answered Oct 13 '22 01:10

Matt Harrison


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

(By this time documentation is not updated yet)

like image 38
coquin Avatar answered Oct 13 '22 03:10

coquin