javascript hapi 在发送响应之前设置标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26213255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:40:31  来源:igfitidea点击:

hapi set header before sending response

javascriptnode.jsheaderhandlerhapijs

提问by CarolineBda

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

在 hapi 处理程序中,我尝试在发回视图之前在代码中更早地设置响应的标头。

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

{....}

reply.view('myView', myContext);

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

你一定要用我的hold方法吗?在这种情况下,如何在渲染视图时重用响应?

Thanks for your help.

谢谢你的帮助。

回答by Roman Rhrn Nesterov

set header on each response
currently tested in hapi 6.6.0


在 hapi 6.6.0 中当前测试的每个响应上设置标头

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

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

 reply();

});

回答by Antony Jones

You can use the hold method as follows

可以使用hold方法如下

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

or even

甚至

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

the reply is held until you call the .send() method, hence:

在您调用 .send() 方法之前,回复将一直保留,因此:

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

is probably what you are looking for.

可能是您正在寻找的。

回答by Mishel Parkour

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

/**************** 自 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

自 hapi v.17 以来的重大变化真正的重大变化是大多数代码和库以及 api 发生了变化,并且上一个样板或指南无济于事。所以你需要寻找标记为 hapi v.17 的新文章

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

api 页面:https: //hapijs.com/api#response-toolkit

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

Firstreply()无效,您应该使用reply.response()

Second in new guides the replyis changed with hits argument so can be named anything but as the guides are using hso you may use h as well.

第二个在新指南中,reply随着h它的参数而改变,所以可以命名任何东西,但正如指南所使用的h那样,你也可以使用 h 。

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

第三,hold()没有定义,很好,不需要。

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

第四,send()我认为不需要甚至没有定义。

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

以及其他一些变化。请检查上面的api链接。

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

所以这是我写的代码的一部分,应该提供一些很好的信息。不关心整个函数只看 h 和响应部分

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;
        }
}

回答by CarolineBda

Ok so this is what I've done (not sure it is the best solution):

好的,这就是我所做的(不确定这是最好的解决方案):

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

That works

这样可行

回答by doodlemoonch

You should be able to use

你应该可以使用

var response = request.view('myView', myContext).header('cache-control: no-cache').hold();

// other stuff

response.send();