MarcoS
MarcoS

Reputation: 17721

Node.js: how to request an image only if it changed

I'm designing a node.js app.
One of its tasks is to regularly download a set of images from some public, external, site.
One requirement is to avoid repeating the download of images which are not changed from the previous download.
I plan to use "request" module, since it is far more complete and flexible with respect to other networking modules (please correct me if I'm wrong).

This is the code I'm using now (please ignore some mistakes, like comparing dates with > or < operators, consider it pseudo-code...):

var request = require('request');
var myResource = {
  'url': 'http://www.example.com/image1.jpg',
  'last-modified': 'Mon, 28 Sep 2015 08:44:06 GMT'
};

request(
  myResource.url,
  { method: 'HEAD'},
  function (err, res, body) {
    if (err) {
      return console.error('error requesting header:', err);
    }
    var lastModifiedDate = res.headers['last-modified'];
    console.log('last modified date:', lastModifiedDate);
    if (lastModifiedDate > myResource['last-modified']) { // resource did change
      request(
        myResource.url,
        function (err, response, contents) {
          if (err) {
            return console.error('error requesting content:', err);
          }
          myResource['last-modified'] = lastModifiedDate;
          storeContents(contents); // store contents to DB
        }
      );
    }
  }
);

This code should work (in principle).
But I ask: request() is called twice: is this a waste of resources?
Could the content request be someway chained to the first request?
Can you suggest a cleaner / smarter / faster approach?

Upvotes: 4

Views: 1481

Answers (1)

Josh C.
Josh C.

Reputation: 4363

Maybe i'm missing something, but if you know the last-modified date, you should send that as the If-Modified-Since header with the GET request and skip the HEAD request. The server should return a 304 when appropriate.

How "304 Not Modified" works?

Upvotes: 3

Related Questions