![](/img/trans.png)
[英]Cloudflare not caching files even when headers are “Cache-Control: public, max-age=86400”
[英]How to get max-age value from cache-control header using request in Nodejs?
我正在使用 request 请求带有以下代码的页面:
request(url, { json: true }, (err, res, body) => {
if (err) {
return null
} else {
console.log("res", res.headers["cache-control"]);
}
})
我可以通过这种方式获取缓存控制标头。 但是如何获取缓存控制标头中的max-age
值,例如cache-control: public, max-age=20140, must-revalidate, no-transform
? 是否有任何速记或我必须进行字符串操作?
没有速记 - 标头只是键/值对,JS 中没有标准库来解析结构化标头值。
您可以使用正则表达式来提取数字:
const headers = { 'cache-control': 'public, max-age=20140, must-revalidate, no-transform' } const matches = headers['cache-control'].match(/max-age=(\\d+)/) const maxAge = matches ? parseInt(matches[1], 10) : -1 console.log(maxAge)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.