Hapi ' s reply interface is one of it's most powerful features. It's smart enough to detect and serialize objects, buffers, promises and even streams. This post would demonstrate first hand how to use the Reply method to send your data to the client, no matter the format.
' Use strict 'Const HAPI= Require (' Hapi ') Const Boom= Require (' boom ')) const Server=Newhapi.server () server.connection ({port:8000}) Server.route ({method:' GET ', Path:‘/‘, Handler:function(Request, Reply) {reply ()//When called without arguments, Hapi responds with a and empty payload. //reply (null, ' Hello World ')//reply'll inspect the first arguments type, only responding with an error if the first argument is in fact an error. Otherwise, it assumes that it should is the response payload. this isn't an error //reply (' Hello World ') //reply ({hello: ' World '}) //Reply (promise.resolve (' Hello World ')) //reply (Require (' FS '). Createreadstream (__filename)) //Reply (new error (' oops '))//if I pass an Error object to reply, Hapi would respond to the client with a error. //reply (Boom.notfound ())}}) Server.start (()= = {})
[Hapi.js] Replying to requests