Rayjax
Rayjax

Reputation: 7784

Node.js server - no caching and cannot access image properties on chrome

I've just started using node, I want to build a single page web app with socket IO. I've built a simple server code :

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'
        });  
   }
}

I serve all files like this, there are other "else if" cases for serving html, css and js files...

With mozilla everything is OK. With Chrome, i run into two problems :

No caching -> the same images are requested everytime i use them in my client side js code, even if i do some preload with an Image() object. Cannot access loaded images properties -> I can't get image.width() or height (returns 0). On firefox caching works and I can get image properties.

With an apache server everything worked fine on both browsers.

I'm totally stuck, got any idea ?

Upvotes: 0

Views: 461

Answers (2)

Rayjax
Rayjax

Reputation: 7784

res.writeHead(200, {
    "Cache-Control" : "max-age=86400",
    'Content-Type': contentType
});

Problem solved (both) ! But does it mean that if a user disables his cache, he spams my server with tons of file requests ?

Is there any way of storing files locally apart from cache ? Like storing images in JS variables ?

Or some other way of achieving that ?

Upvotes: 1

Zyberzero
Zyberzero

Reputation: 1604

Regardings the cache, try to set the cache headers explicit in your code for your static files.

Upvotes: 1

Related Questions