Connect

Static

Static file server with the given root path.

Examples

var oneDay = 86400000;

connect()
  .use(connect.static(__dirname + '/public'))

connect()
  .use(connect.static(__dirname + '/public', { maxAge: oneDay }))

Options

  • maxAge Browser cache maxAge in milliseconds. defaults to 0
  • hidden Allow transfer of hidden files. defaults to false
  • redirect Redirect to trailing "/" when the pathname is a dir. defaults to true

Source

exports = module.exports = function static(root, options){
  options = options || {};

  // root required
  if (!root) throw new Error('static() root path required');

  // default redirect
  var redirect = false === options.redirect ? false : true;

  return function static(req, res, next) {
    if ('GET' != req.method && 'HEAD' != req.method) return next();
    var path = parse(req).pathname;
    var pause = utils.pause(req);

    function resume() {
      next();
      pause.resume();
    }

    function directory() {
      if (!redirect) return resume();
      var pathname = url.parse(req.originalUrl).pathname;
      res.statusCode = 301;
      res.setHeader('Location', pathname + '/');
      res.end('Redirecting to ' + utils.escape(pathname) + '/');
    }

    function error(err) {
      if (404 == err.status) return resume();
      next(err);
    }

    send(req, path)
      .maxage(options.maxAge || 0)
      .root(root)
      .hidden(options.hidden)
      .on('error', error)
      .on('directory', directory)
      .pipe(res);
  };
};

exports.mime

Expose mime module.

If you wish to extend the mime table use this
reference to the "mime" module in the npm registry.

Source

exports.mime = send.mime;