在hapi中设置缓存头

时间:2016-02-26 05:59:34

标签: node.js caching express http-headers 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'
}

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

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

2 个答案:

答案 0 :(得分:7)

是的,你可以做到。该字符串('no-cache, no-store, must-revalidate')只是标题的单个值,因此将其设置为任何标题。通过调用response object上的header()方法。

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

        reply('ok').header('Cache-Control', 'no-cache, no-store, must-revalidate');
    }
});

答案 1 :(得分:0)

在hapi v17和v18中,您可以通过这种方式设置标头

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, h) {
    return h.response('ok').header('Cache-Control', 'no-cache, no-store, must-revalidate');
  }
});

文档:https://hapi.dev/tutorials/caching/?lang=en_US