hapi set header before sending response

CarolineBda picture CarolineBda · Oct 6, 2014 · Viewed 18.4k times · Source

In a hapi handler I try to set a header of my response earlier in the code before sending back a view.

reply().header('cache-control', 'no-cache');

{....}

reply.view('myView', myContext);

Do you I have to use the hold method? In that case how do I reuse the response when rendering the view?

Thanks for your help.

Answer

Roman Rhrn Nesterov picture Roman Rhrn Nesterov · Dec 12, 2014

set header on each response
currently tested in hapi 6.6.0

server.ext('onPreResponse', function(request, reply) {

 request.response.header('X-API-VERSION', '0.0.1');

 reply();

});