Node.js: 如何仅在图像发生变化时请求图像

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

我正在设计 node.js 应用。
它的任务之一是定期从某些 public、外部、站点下载一组图像。
一个要求是避免重复下载与上次下载相比没有变化的图像。
我打算使用 "request" 模块,因为相对于其他网络模块,它更加完整和灵活(如果我错了,请纠正我)。

这是我现在使用的代码(请忽略一些错误,比如比较日期与><运算符,认为它是伪代码...):

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

此代码应该有效(原则上)。
但是请问:request()被调用了两次:这是不是浪费资源?
内容请求能否以某种方式链接到第一个请求?
你能建议一种更清洁/更智能/更快的方法吗?

也许我遗漏了什么,但如果您知道 last-modified 日期,您应该将其作为 If-Modified-Since header 与 GET 请求一起发送并跳过HEAD 请求。服务器应该 return a 304 在适当的时候。

How "304 Not Modified" works?