zoukankan      html  css  js  c++  java
  • [Hapi.js] Friendly error pages with extension events

    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 )
    } );
  • 相关阅读:
    bzoj 4610 Ceiling Functi
    uva 01350
    uva 12075
    uva 01393
    uva 11038
    CF 496E
    CF 496D
    poj 3167
    hdu 4622
    spoj 7258
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5226230.html
Copyright © 2011-2022 走看看