从缓存 Node JS 提供内容

serve content from cache Node JS

晚安 我正在尝试从缓存内存中提供内容,我正在使用在线课程学习,但我遇到了一个问题,因为当我修改内容文件时,服务继续使用缓存内存并且我使用条件来评估缓存是否更新....

我在 Linux 中使用了此代码并且它起作用了,但是当我使用 windows 时它不起作用...

 var http = require('http');
 var path = require('path');
 var fs = require('fs');

 var mimeTypes = {
 '.js' : 'text/javascript',
 '.html': 'text/html',
 '.css' : 'text/css'
  };

  var cache = {};
  function cacheYEntrega(f, cb) {
  fs.stat(f, function (err, stats) {
  var ultimoCambio = Date.parse(stats.ctime),
  estaActualizado = (cache[f]) && ultimoCambio  > cache[f].timestamp;
  if (!cache[f] || estaActualizado) {
  fs.readFile(f, function (err, data) {
    console.log('cargando ' + f + ' desde archivo');
    if (!err) {
      cache[f] = {content: data,
                  timestamp: Date.now() //almacenar datos tiempo actual
                 };
    }
    cb(err, data);
  });
  return;
  }
  console.log('cargando ' + f + ' de cache');
  cb(null, cache[f].content);
  }); //final de fs.stat
   }

   http.createServer(function (request, response) {
   var buscar= path.basename(decodeURI(request.url)) || 'index.html',
   f = 'content/' + buscar;
   fs.exists(f, function (exists) { //path.exists para Node 0.6 e inferiores
   if (exists) {

   cacheYEntrega(f, function (err, data) {
    if (err) {response.writeHead(500); response.end(); return; }
    var headers = {'Content-type': mimeTypes[path.extname(f)]};
    response.writeHead(200, headers);
    response.end(data);

     });
     return;

     }
    response.writeHead(404); //no se ha encontrado archivo
    response.end('Pagina no encontrada');

     });

      }).listen(8080);

谢谢

查看代码和变量后发现stats的ctime有问题,不知道为什么修改文件内容后ctime没有变化,所以只用了mtime而不是ctime 并开始以正确的方式提供内容.... 谢谢...