node.js - Node/Express serving image if exist; Caching issue -


been using while it's first question here, couldn't find solution particular problem. i'll try explicit possible, unclear let me know update question accordingly.

i'm using route serve user's photo according userid passed in route parameter (). works, each time tag encountered request image server , never uses cached version.

i played cache-control bit, it's either or nothing: if caching enabled , user change photo, still use old photo.

my question is: possible use cached version if photo hasn't changed, if did use 1 server? tried using "must-revalidate" along "max-age" or "no-cache" no avail.

here's route code if helps:

module.exports.getphoto = function(req, res) {     var mime = require('mime-magic'),         memberid = req.params.memberid,         imgurl;      path.exists('public/images/memberphotos/' + memberid, function(exists) {         if(exists) {             imgurl = 'public/images/memberphotos/' + memberid;         }else{             imgurl = 'public/images/memberphotos/nophoto.jpg';         }          fs.readfile(imgurl, function(err, img) {             mime.filewrapper(imgurl, function(err, mimetype) {                 if(!err) {                     console.log(mimetype);                     res.writehead(200, {                         'content-type': mimetype,                         'cache-control': "max-age=" + 43800*60 + ", must-revalidate"                     });                     res.end(img, 'binary');                  }             });          });     }); }; 

thanks

you want use etag , if-none-match headers. need track images etags on server , if incoming if-none-match matches current etag respond request 304 - not modified. otherwise serve image , include new etag in headers.

see http://blog.phyber.com/supporting-cache-controls-in-nodejs more details.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -