Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:56:33+00:00 2026-06-05T03:56:33+00:00

been using SO for a while but it’s my first question here, as I

  • 0

been using SO for a while but it’s my first question here, as I couldn’t find a solution for this particular problem. I’ll try to be as explicit as possible, is anything is unclear just let me know so I update the question accordingly.

I’m using a route to serve a user’s photo according to the userId passed in the route as parameter (). It works, but each time the tag is encountered it request the image from the server and never uses a cached version.

I played with Cache-Control a bit, but it’s either all or nothing: If caching is enabled and the user change his photo, it will still use the old photo.

My question is: is it possible to use a cached version if the photo hasn’t changed, but if it did use the one from the server? I tried using “must-revalidate” along with “max-age” or “no-cache” to no avail.

Here’s the route code if it 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:56:35+00:00Added an answer on June 5, 2026 at 3:56 am

    You want to use an ETag and If-None-Match headers. You will need to track your images ETags on the server and if the incoming If-None-Match matches the current ETag you respond to the request with a 304 – Not Modified. Otherwise you serve the image and include a new ETag in the headers.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been googling for a while but couldn't find a solution for my problem.
This is crazy... been using Spring for a while but can't find something like
for a while I've been using the simple_html_dom include, but I have a question.
Very easy question. I have been using CSS for a while but rarely use
I have been using a while loop for this but recently got into debate
I have been using this for a while, but have since then upgraded to
I have been using emacs for a while but not so familiar with lisp
I have been using the CodeIgniter system for a while now - but it
I've been successfully using Django's admin panel for internal users for a while but
I have been using PHPUnit for a while now, but suddenly hit a big

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.