在 hapi 中设置缓存 header

Set cache header in hapi

如何将hapi中的cache-controlheader设置为'no-cache'、'no-store'、'must-revalidate'?

在 express 中,我可以执行以下操作: 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');
}

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

上调用 header() 方法
server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

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

在 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