lib/fileserver.js
function createListener(virtualPath) {

  return function listener(req, res, next) {
    var paths = allPaths[virtualPath].slice();

    function processRequest(paths) {

      var physicalPath = paths.pop()
        , synchronizer = new Synchronizer()
        , uri = urlParser.parse(req.url).pathname
        , filename = "";

      if (virtualPath !== "/" && uri.indexOf(virtualPath) === 0) {
        uri = uri.substr(virtualPath.length, uri.length - virtualPath.length);
      }
      uri = (physicalPath || "") + uri;

      filename = fs.combine(process.cwd(), uri).replace(/\%20/gmi, ' ');

      if (cache[filename]) {
        console.log("\u001b[32mLoaded from Cache '" + filename + "\u001b[39m'");
        if (cache[filename] === 404) {
          res.writeHead(404, {"Content-Type": "text/plain"});
          res.write("Content not found");
          res.end();
        } else {
          if (req.headers["If-Modified-Since"] === cache[filename].since) {
            res.writeHead(304, {
              'Content-Type': mime.lookup(filename),
              'Last-Modified': cache[filename].since,
              //'If-Modified-Since': cache[filename].since,
              "Cache-Control": "max-age=31536000"
            });
            res.end();
          } else {
            res.writeHead(200, {
              'Content-Type': mime.lookup(filename),
              //'If-Modified-Since': cache[filename].since,
              'Last-Modified': cache[filename].since,
              "Cache-Control": "max-age=31536000"
            });
            res.write(cache[filename].data, "binary");
            res.end();
          }
        }
        return;
      }

      if (filename) {
        fs.readFile(filename, "binary", synchronizer.register("file"));
      }

      synchronizer.onfinish(function (err, data) {
        if (err) {
          if (paths.length > 0) {
            processRequest(paths);
          } else if (next) {
            next();
          } else {
            console.log("\u001b[31mStatic file not found '" + filename + "'\u001b[39m");
            cache[filename] = 404;
            res.writeHead(404, {"Content-Type": "text/plain"});
            res.write("Content not found");
            res.end();
          }
          return;
        }
        console.error("\u001b[31mLoaded static file '" + filename + "'\u001b[39m");
        cache[filename] = {
          data: data.file,
          since: new Date().toGMTString()
        };
        res.writeHead(200, {
          'Content-Type': mime.lookup(filename),
          //'If-Modified-Since': cache[filename].since,
          'Last-Modified': cache[filename].since,
          "Cache-Control": "max-age=31536000"
        });
        res.write(data.file, "binary");
        res.end();
        if (!settings.cacheEnabled) { cache[filename] = undefined; }
      });
    }

    processRequest(paths);
  };

}
function createListener(virtualPath) {

  return function listener(req, res, next) {
    var paths = allPaths[virtualPath].slice();

    function processRequest(paths) {

      var physicalPath = paths.pop()
        , synchronizer = new Synchronizer()
        , uri = urlParser.parse(req.url).pathname
        , filename = "";

      if (virtualPath !== "/" && uri.indexOf(virtualPath) === 0) {
        uri = uri.substr(virtualPath.length, uri.length - virtualPath.length);
      }
      uri = (physicalPath || "") + uri;

      filename = fs.combine(process.cwd(), uri).replace(/\%20/gmi, ' ');

      if (cache[filename]) {
        console.log("\u001b[32mLoaded from Cache '" + filename + "\u001b[39m'");
        if (cache[filename] === 404) {
          res.writeHead(404, {"Content-Type": "text/plain"});
          res.write("Content not found");
          res.end();
        } else {
          if (req.headers["If-Modified-Since"] === cache[filename].since) {
            res.writeHead(304, {
              'Content-Type': mime.lookup(filename),
              'Last-Modified': cache[filename].since,
              //'If-Modified-Since': cache[filename].since,
              "Cache-Control": "max-age=31536000"
            });
            res.end();
          } else {
            res.writeHead(200, {
              'Content-Type': mime.lookup(filename),
              //'If-Modified-Since': cache[filename].since,
              'Last-Modified': cache[filename].since,
              "Cache-Control": "max-age=31536000"
            });
            res.write(cache[filename].data, "binary");
            res.end();
          }
        }
        return;
      }

      if (filename) {
        fs.readFile(filename, "binary", synchronizer.register("file"));
      }

      synchronizer.onfinish(function (err, data) {
        if (err) {
          if (paths.length > 0) {
            processRequest(paths);
          } else if (next) {
            next();
          } else {
            console.log("\u001b[31mStatic file not found '" + filename + "'\u001b[39m");
            cache[filename] = 404;
            res.writeHead(404, {"Content-Type": "text/plain"});
            res.write("Content not found");
            res.end();
          }
          return;
        }
        console.error("\u001b[31mLoaded static file '" + filename + "'\u001b[39m");
        cache[filename] = {
          data: data.file,
          since: new Date().toGMTString()
        };
        res.writeHead(200, {
          'Content-Type': mime.lookup(filename),
          //'If-Modified-Since': cache[filename].since,
          'Last-Modified': cache[filename].since,
          "Cache-Control": "max-age=31536000"
        });
        res.write(data.file, "binary");
        res.end();
        if (!settings.cacheEnabled) { cache[filename] = undefined; }
      });
    }

    processRequest(paths);
  };

}
function createListener ( virtualPath ) { return function listener ( req , res , next ) { var paths = allPaths [ virtualPath ] . slice ( ) ; function processRequest ( paths ) { var physicalPath = paths . pop ( ) , synchronizer = new Synchronizer ( ) , uri = urlParser . parse ( req . url ) . pathname , filename = "" ; if ( virtualPath !== "/" && uri . indexOf ( virtualPath ) === 0 ) { uri = uri . substr ( virtualPath . length , uri . length - virtualPath . length ) ; } uri = ( physicalPath || "" ) + uri ; filename = fs . combine ( process . cwd ( ) , uri ) . replace ( / \%20 / gmi , ' ' ) ; if ( cache [ filename ] ) { console . log ( "\u001b[32mLoaded from Cache '" + filename + "\u001b[39m'" ) ; if ( cache [ filename ] === 404 ) { res . writeHead ( 404 , { "Content-Type" : "text/plain" } ) ; res . write ( "Content not found" ) ; res . end ( ) ; } else { if ( req . headers [ "If-Modified-Since" ] === cache [ filename ] . since ) { res . writeHead ( 304 , { 'Content-Type' : mime . lookup ( filename ) , 'Last-Modified' : cache [ filename ] . since , //'If-Modified-Since': cache[filename].since, "Cache-Control" : "max-age=31536000" } ) ; res . end ( ) ; } else { res . writeHead ( 200 , { 'Content-Type' : mime . lookup ( filename ) , //'If-Modified-Since': cache[filename].since, 'Last-Modified' : cache [ filename ] . since , "Cache-Control" : "max-age=31536000" } ) ; res . write ( cache [ filename ] . data , "binary" ) ; res . end ( ) ; } } return ; } if ( filename ) { fs . readFile ( filename , "binary" , synchronizer . register ( "file" ) ) ; } synchronizer . onfinish ( function ( err , data ) { if ( err ) { if ( paths . length > 0 ) { processRequest ( paths ) ; } else if ( next ) { next ( ) ; } else { console . log ( "\u001b[31mStatic file not found '" + filename + "'\u001b[39m" ) ; cache [ filename ] = 404 ; res . writeHead ( 404 , { "Content-Type" : "text/plain" } ) ; res . write ( "Content not found" ) ; res . end ( ) ; } return ; } console . error ( "\u001b[31mLoaded static file '" + filename + "'\u001b[39m" ) ; cache [ filename ] = { data : data . file , since : new Date ( ) . toGMTString ( ) } ; res . writeHead ( 200 , { 'Content-Type' : mime . lookup ( filename ) , //'If-Modified-Since': cache[filename].since, 'Last-Modified' : cache [ filename ] . since , "Cache-Control" : "max-age=31536000" } ) ; res . write ( data . file , "binary" ) ; res . end ( ) ; if ( ! settings . cacheEnabled ) { cache [ filename ] = undefined ; } } ) ; } processRequest ( paths ) ; } ; }
Create a new static file handler
@param virtualPath
@param physicalPath
Create a new static file handler