【问题标题】:Enable caching for download method of Loopback Storage Component?为 Loopback Storage Component 的下载方法启用缓存?
【发布时间】:2016-11-09 11:59:42
【问题描述】:

我使用 Loopback 作为后端 API,并且还使用 Storage 组件作为 CDN 来为我的网站上传和下载图像和声音文件。 我的网站使用了很多图片。但是所有图像文件都没有启用缓存。

我想通过在文件中添加“Cache-Control:max-age=2678400”标头来启用缓存,但不知道该怎么做。有人可以帮助我或建议任何更好的解决方案。我真的很感激。

谢谢!

【问题讨论】:

    标签: caching loopbackjs


    【解决方案1】:

    最后我找到了使用中间件的解决方法。 在 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": {}
    }
    ...
    

    希望对您有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2015-05-20
      • 2015-09-11
      • 2016-03-05
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多