[Hapi.js] Friendly error pages with extension events
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 ) } );
以上是关于[Hapi.js] Friendly error pages with extension events的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 dropzone.js 和 hapi.js 上传图片
“502 Bad Gateway”将 hapi.js 部署到 AWS Beanstalk?