【问题标题】:Set cache header in hapi在 hapi 中设置缓存头
【发布时间】:2016-06-09 05:57:07
【问题描述】:

如何将 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');
}

【问题讨论】:

  • 如何强制缓存某些文件?

标签: node.js caching express http-headers hapijs


【解决方案1】:

是的,你可以做到。该字符串 ('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');
    }
});

【讨论】:

  • 太棒了。感谢您的确认。
  • 你如何为所有路线做到这一点?
  • 您可以在路由生命周期扩展点中设置标头。查看 hapi 文档中的 onPreResponse
【解决方案2】:

在 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

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 2011-01-28
    • 2023-03-15
    • 2014-11-30
    • 2011-12-10
    • 2012-02-06
    • 2013-09-28
    • 2018-12-29
    • 2018-06-19
    相关资源
    最近更新 更多