Skip to content

Instantly share code, notes, and snippets.

@kevboutin
Last active February 26, 2016 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevboutin/3a26dccf65bdba4ded92 to your computer and use it in GitHub Desktop.
Save kevboutin/3a26dccf65bdba4ded92 to your computer and use it in GitHub Desktop.
Shows how to configure and use friendly error pages using extension events in hapi within node.
'use strict';
const Hapi = require('hapi');
const Boom = require('boom');
const server = new Hapi.Server();
server.connection({ port: 8000 });
// test by using this in browser:
// http://localhost:8000
server.register(require('vision'), () => {
server.views({
engines: { hbs: require('handlebars') },
relativeTo: __dirname,
path: 'views'
});
server.ext('onPreResponse', (request, response) => {
let resp = request.response;
if (!resp.isBoom) return response.continue();
response.view('error', resp.output.payload)
.code(resp.output.statusCode);
});
server.route({
method: 'GET',
path: '/',
config: {
handler: function(request, response) {
response(Boom.notFound());
}
}
});
server.start(() => console.log(`Started at ${server.info.uri}`));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment