Last active
February 26, 2016 21:21
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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