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=Newhapi.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) {varRESP =Request.response; //If there is an error if(resp.isboom) {returnReply.view (' ERROR ', Resp.output.payload)//Change the status code. Code (RESP.OUTPUT.STATUSCODE); } reply.Continue()})}) Server.start (function(err) {if(err) {Throwerr; } console.log (' Started at: ', Server.info.uri)} );
[Hapi.js] Friendly error pages with extension events