繁体   English   中英

如何使用Nodejs中的请求从缓存控制标头中获取max-age值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM