为环回存储组件的下载方法启用缓存



我使用环回作为后端API,也使用存储组件作为CDN上传和下载图像和声音文件为我的网站。我的网站用了很多图片。但是并不是所有的映像文件都支持缓存。

我想通过添加"cache - control:max-age=2678400"头文件来启用缓存,但不知道如何做到这一点。有人能帮助我或建议更好的解决方案吗?我真的很感激。

谢谢!

最后我找到了一个使用中间件的解决方案。在server/middleware文件夹中创建一个中间件:

// cache.js
module.exports = function () {
    return function cacheImages(req, res, next) {
        // Check if download file:
        if (req.originalUrl.includes('/api/files/') && req.originalUrl.includes('/download/')) {
            console.log("Here at the middle ware");
            console.log(req.originalUrl);
            res.set('Cache-Control', 'max-age=315360000');
        }
        next();
    }
}

并在server/middleware中添加此中间件。Json配置文件:

...
"initial": {
    "./middleware/cache": {}
}
...

希望这有帮助!:)

最新更新