Exemplo n.º 1
0
 return function (req, res, next) {
   
   if (!routeDefinitions) {
     return next();
   }
   
   var pathname = url.parse(req.url).pathname;
   var defs = {};
   
   // Ensure order of routes when given an array
   if (Array.isArray(routeDefinitions)) {
     routeDefinitions
       .forEach(function (routes) {
         
         Object.keys(routes).forEach(function (key) {
           
           defs[key] = routes[key];
         })
       });
   }
   else {
     defs = routeDefinitions;
   }
 
   var routes = globject(slasher(defs));
   var filepath = routes(slasher(pathname));
   
   if (!filepath) {
     return next();
   }
   
   filepath = directoryIndex(filepath, indexFile);
   
   if (!fileExists(filepath, {root: root})) {
     return next();
   }
   
   req.url = filepath;
   
   if (options.fullPath) {
     var p = options.fullPath(filepath);
     root = p.root;
     req.url = p.pathname;
   }
   
   deliver(req, res, {
     root: root,
     index: indexFile,
     contentType: mime.lookup(filepath),
     headers: options.headers
   }).pipe(res);
 };
Exemplo n.º 2
0
 return function (req, res, next) {
   if (!req.config) return next();
   
   var pathname = url.parse(req.url).pathname;
   var headersConfig = globject(slash(req.config.headers));
   var headers = headersConfig(slash(pathname)) || {};
   
   _.each(headers, function (val, name) {
     res.setHeader(name, val);
   });
   
   return next();
 };
Exemplo n.º 3
0
 return function (req, res, next) {
   
   var pathname = url.parse(req.url).pathname;
   var cacheValues = globject(slasher(cachePaths || {}, {value: false}));
   var cacheValue = cacheValues(slasher(pathname));
   
   onHeaders(res, function () {
     
     // Default value
     res.setHeader('Cache-Control', 'public, max-age=300');
     setCacheHeader(res, cacheValue);
   });
   
   next();
 };
Exemplo n.º 4
0
module.exports = function (imports) {
  
  var routeDefinitions = flattenToObject(imports.routes);
  var routes = globject(slasher(routeDefinitions));
  
  return function (req, res, next) {
    
    var pathname = url.parse(req.url).pathname;
    var filepath = routes(slasher(pathname));
    
    if (!filepath) {
      return next();
    }
    
    res.__.sendFile(filepath)
      .on('error', function () {
        
        next();
      });
  };
};