Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hapi set header before sending response

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.

like image 933
CarolineBda Avatar asked Oct 06 '14 09:10

CarolineBda


People also ask

Can we set response header?

And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.

How do I add a header in Hapijs?

js – We set header with Content-Type and Content-Disposition in response and send that csv data as the binary format. set header in hapi. js – We send that csv file in response and then set header with Content-Type and Content-Disposition.

What is a response header in Javascript?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.

What is Hapi in Nodejs?

Hapi. js (derived from Http-API) is an open-source Node. js framework used to build powerful and scalable web applications. Hapi is commonly used to build Application Programming Interface servers, HTTP-proxy applications, and websites. Hapi.


1 Answers

/**************** BREAKING CHANGES SINCE HAPI V.17 ****************/

Breaking changes since hapi v.17 real breaking changes most of codes and libraries and apis changed and prev boiler plates or guides cant help much. So you need to look for new articles tagged with hapi v.17

api page: https://hapijs.com/api#response-toolkit

First reply() is not valid and you should use reply.response()

Second in new guides the reply is changed with h its argument so can be named anything but as the guides are using h so you may use h as well.

Third the hold() is not defined and well and its not needed.

Forth the send() is not needed or even not define i think.

And some other changes. Please check the api link above.

so this is part of code I wrote should give some good information. dont care about the whole function just look at the h and the response section

static _json_response(res, request = null, h = null){
        let ret = '';
        ret = JSON.stringify(res);
        if (request == null || h == null){
                return ret;
        }else{
                const response = h.response(ret)
                        .header('cache-control', 'no-cache')
                        .type('application/json')
                return response;
        }
}
like image 56
Mishel Parkour Avatar answered Oct 02 '22 20:10

Mishel Parkour