码迷,mamicode.com
首页 > Windows程序 > 详细

[Hapi.js] Friendly error pages with extension events

时间:2016-02-29 07:05:44      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

hapi automatically responds with JSON for any error passed to a route‘s reply()method. But what if your application needs errors rendered in HTML? This lesson shows how to implement friendly HTML error messages using hapi extension events.

 

const Hapi = require( ‘hapi‘ )
const Boom = require( ‘boom‘ )
const server = new Hapi.Server()
server.connection( { port: 8000 } )


server.register(require(‘vision‘), function(request, reply){
    server.views({
        engines: { hbs: require(‘handlebars‘) },
        relativeTo: __dirname,
        path: ‘views‘
    });

    server.ext(‘onPreResponse‘, function(request, reply){
        var resp = request.response;
        // if there is an error
        if (resp.isBoom) {
            return reply.view(‘error‘, resp.output.payload)
                        // change the status code
                        .code(resp.output.statusCode);
        }
        reply.continue()
    })
})


server.start( function (err) {
    if (err) {
        throw err;
    }
    console.log( ‘Started at:‘, server.info.uri )
} );

 

[Hapi.js] Friendly error pages with extension events

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5226230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!