Hapi set a header before sending a response

In the hapi handler, I am trying to set the title of my response earlier in the code before submitting the view.

reply().header('cache-control', 'no-cache'); {....} reply.view('myView', myContext); 

Do you need to use the hold method? In this case, how to reuse the response when rendering the view?

Thanks for your help.

+5
source share
4 answers

set a title for each answer
currently being tested in hapi 6.6.0

 server.ext('onPreResponse', function(request, reply) { request.response.header('X-API-VERSION', '0.0.1'); reply(); }); 
+6
source

You can use the hold method as follows

 reply.hold(); reply.view('your-view'); 

or even

 reply.view('your-view').hold(); reply.send(); 

the response is saved until the .send () method is called, therefore:

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

probably what you are looking for.

+4
source

You should be able to use

 var response = request.view('myView', myContext).header('cache-control: no-cache').hold(); // other stuff response.send(); 
0
source

Ok, so here is what I did (not sure if this is the best solution):

 var response; {...} response = reply().header('cache-control', 'no-cache').hold(); {...} response = response || reply.view('summary', summary).hold(); response.send(); 

It works

0
source

Source: https://habr.com/ru/post/1204088/


All Articles