Exemplo n.º 1
0
    watcher.on('all', function (ev, file) {
      if (file.stat.isDirectory() || file.name.match(/^\./)) return;
      var urlPath = file.fullPath.replace(root, '');
      switch (ev) {
        case 'add':
        case 'update':
          try {
            cache[urlPath] = dish.file(file.fullPath, options);
            // Index support
            if (isIndex(urlPath)) {
              cache[path.dirname(urlPath)] = cache[urlPath];
            }
          }
          catch (e) {
            delete cache[urlPath];
          }
          break;
        case 'remove':
          delete cache[urlPath];
          if (isIndex(urlPath)) {
            delete cache[path.dirname(urlPath)];
          }
          break;
      }

    });
Exemplo n.º 2
0
 function checkCache () {
   var urlPath = parsedUrls[req.url];
   if (!urlPath) {
     urlPath = parseUrl(req.url).pathname;
     // Decode and strip nullbytes for security
     try {
       urlPath = decodeURIComponent(urlPath).replace(/\0/g, '');
     }
     catch (err) {
       if (err.message === 'URI malformed') {
         res.writeHead(400);
         return res.end();
       }
       if (!next) throw err;
       return next(err);
     }
     parsedUrls[req.url] = urlPath;
   }
   var file = self.get(urlPath);
   if (file && file.stat.isFile()) return dish.file(file.fullPath, options)(req, res, next);
   if (options.index) {
     if (urlPath !== '/') urlPath += '/';
     urlPath += options.index;
     file = self.get(urlPath);
     if (file) return file.serve(options)(req, res, next);
   }
   next && next();
 }
Exemplo n.º 3
0
 matches.forEach(function(filePath) {
   if (/\/$/.exec(filePath)) return;
   var urlPath = filePath.substring(root.length);
   try {
     cache[urlPath] = dish.file(filePath, options);
     // Index support
     if (isIndex(urlPath)) {
       cache[path.dirname(urlPath)] = cache[urlPath];
     }
   }
   catch (e) {
     delete cache[urlPath];
   }
 });
Exemplo n.º 4
0
  mw.notFound = function (req, res, next) {
    if (req.method !== 'GET' && req.method !== 'HEAD') {
      res.writeHead(405, {'Content-Type': 'text/plain; charset=utf-8'});
      res.end('Method not allowed\n');
    }
    else {
      if (options.notFoundPath) {
        var cached = self.get(options.notFoundPath);
        if (cached) dish.file(cached.fullPath, {status: 404})(req, res, next);
        else default404();
      }
      else default404();

      function default404 () {
        res.writeHead(404, {'Content-Type': 'text/plain; charset=utf-8'});
        res.end('Page not found\n');
        return;
      }
    }
  };