在hapi中设置缓存头

Chr*_*ith 3 caching http-headers node.js express hapijs

如何将hapi中的缓存控制头设置为"no-cache","no-store","must-revalidate"?

在快递中,我能够做到以下几点: res.header('Cache-Control', 'no-cache, no-store, must-revalidate');

我目前在hapi中有以下内容,但我认为它可能不正确:

function(request, reply){
  var response = reply();
  response.header('Cache-Control', 'no-cache');
  response.header('Cache-Control', 'no-store');
  response.header('Cache-Control', 'must-revalidate'
}
Run Code Online (Sandbox Code Playgroud)

是否可以在hapi中执行此操作?

function(request, reply){
  var response = reply();
  response.header('Cache-Control', 'no-cache, no-store, must-revalidate');
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*son 7

是的,你可以做到.string('no-cache, no-store, must-revalidate')只是标题的单个值,因此将其设置为任何标题.通过调用响应对象header()上的方法.

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        reply('ok').header('Cache-Control', 'no-cache, no-store, must-revalidate');
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 您可以在路由生命周期扩展点中设置标头.看一下hapi文档中的`onPreResponse` (2认同)