【问题标题】:Node.js server - no caching and cannot access image properties on chromeNode.js 服务器 - 没有缓存并且无法访问 chrome 上的图像属性
【发布时间】:2012-09-25 20:10:47
【问题描述】:

我刚开始使用 node,我想用 socket IO 构建一个单页 web 应用程序。 我已经构建了一个简单的服务器代码:

http.createServer(function(req, res) {

    //Requested url split.
    var pathname = url.parse(req.url).pathname;
    var extension = path.extname(pathname).toLowerCase();

     console.log(pathname + " " + extension);

    if(extension == ".png"){
        res.writeHead(200, {
            'Content-Type': 'image/png'
        });  
   }
}

我提供这样的所有文件,还有其他“else if”案例用于提供 html、css 和 js 文件...

使用 Mozilla 一切正常。 使用 Chrome,我遇到了两个问题:

无缓存 -> 每次我在客户端 js 代码中使用它们时都会请求相同的图像,即使我使用 Image() 对象进行了一些预加载。 无法访问加载的图像属性 -> 我无法获取 image.width() 或高度(返回 0)。 在firefox缓存工作上,我可以获得图像属性。

使用 apache 服务器在两种浏览器上一切正常。

我完全被卡住了,你知道吗?

【问题讨论】:

    标签: javascript node.js caching


    【解决方案1】:

    关于缓存,请尝试在代码中为您的静态文件显式设置缓存标头。

    【讨论】:

      【解决方案2】:
      res.writeHead(200, {
          "Cache-Control" : "max-age=86400",
          'Content-Type': contentType
      });
      

      问题已解决(两者)! 但这是否意味着如果用户禁用他的缓存,他会向我的服务器发送大量文件请求?

      除了缓存之外,还有什么方法可以在本地存储文件吗?就像在 JS 变量中存储图片一样?

      或者其他方式来实现?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-24
        • 2012-04-23
        • 2017-02-10
        • 2011-02-14
        • 2018-08-01
        • 2017-09-29
        • 2019-06-22
        • 1970-01-01
        相关资源
        最近更新 更多