|
| 1 | +/*! |
| 2 | + * Connect - static |
| 3 | + * Copyright(c) 2010 Sencha Inc. |
| 4 | + * Copyright(c) 2011 TJ Holowaychuk |
| 5 | + * Copyright(c) 2014 Douglas Christopher Wilson |
| 6 | + * MIT Licensed |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Module dependencies. |
| 11 | + */ |
| 12 | + |
| 13 | +var send = require('send'); |
| 14 | +var url = require('url'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Static: |
| 18 | + * |
| 19 | + * Static file server with the given `root` path. |
| 20 | + * |
| 21 | + * Examples: |
| 22 | + * |
| 23 | + * var oneDay = 86400000; |
| 24 | + * var serveStatic = require('serve-static'); |
| 25 | + * |
| 26 | + * connect() |
| 27 | + * .use(serveStatic(__dirname + '/public')) |
| 28 | + * |
| 29 | + * connect() |
| 30 | + * .use(serveStatic(__dirname + '/public', { maxAge: oneDay })) |
| 31 | + * |
| 32 | + * Options: |
| 33 | + * |
| 34 | + * - `maxAge` Browser cache maxAge in milliseconds. defaults to 0 |
| 35 | + * - `hidden` Allow transfer of hidden files. defaults to false |
| 36 | + * - `redirect` Redirect to trailing "/" when the pathname is a dir. defaults to true |
| 37 | + * - `index` Default file name, defaults to 'index.html' |
| 38 | + * |
| 39 | + * @param {String} root |
| 40 | + * @param {Object} options |
| 41 | + * @return {Function} |
| 42 | + * @api public |
| 43 | + */ |
| 44 | + |
| 45 | +exports = module.exports = function(root, options){ |
| 46 | + options = options || {}; |
| 47 | + |
| 48 | + // root required |
| 49 | + if (!root) throw new TypeError('root path required'); |
| 50 | + |
| 51 | + // default redirect |
| 52 | + var redirect = false !== options.redirect; |
| 53 | + |
| 54 | + return function staticMiddleware(req, res, next) { |
| 55 | + if ('GET' != req.method && 'HEAD' != req.method) return next(); |
| 56 | + var originalUrl = url.parse(req.originalUrl); |
| 57 | + var path = parse(req).pathname; |
| 58 | + |
| 59 | + if (path == '/' && originalUrl.pathname[originalUrl.pathname.length - 1] != '/') { |
| 60 | + return directory(); |
| 61 | + } |
| 62 | + |
| 63 | + function directory() { |
| 64 | + if (!redirect) return next(); |
| 65 | + var target; |
| 66 | + originalUrl.pathname += '/'; |
| 67 | + target = url.format(originalUrl); |
| 68 | + res.statusCode = 303; |
| 69 | + res.setHeader('Location', target); |
| 70 | + res.end('Redirecting to ' + escape(target)); |
| 71 | + } |
| 72 | + |
| 73 | + function error(err) { |
| 74 | + if (404 == err.status) return next(); |
| 75 | + next(err); |
| 76 | + } |
| 77 | + |
| 78 | + send(req, path) |
| 79 | + .maxage(options.maxAge || 0) |
| 80 | + .root(root) |
| 81 | + .index(options.index || 'index.html') |
| 82 | + .hidden(options.hidden) |
| 83 | + .on('error', error) |
| 84 | + .on('directory', directory) |
| 85 | + .pipe(res); |
| 86 | + }; |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * Escape the given string of `html`. |
| 91 | + * |
| 92 | + * @param {String} html |
| 93 | + * @return {String} |
| 94 | + * @api private |
| 95 | + */ |
| 96 | + |
| 97 | +function escape(html) { |
| 98 | + return String(html) |
| 99 | + .replace(/&(?!\w+;)/g, '&') |
| 100 | + .replace(/</g, '<') |
| 101 | + .replace(/>/g, '>') |
| 102 | + .replace(/"/g, '"'); |
| 103 | +}; |
| 104 | + |
| 105 | +/** |
| 106 | + * Parse the `req` url. |
| 107 | + * |
| 108 | + * @param {ServerRequest} req |
| 109 | + * @return {Object} |
| 110 | + * @api private |
| 111 | + */ |
| 112 | + |
| 113 | +function parse(req) { |
| 114 | + var parsed = url.parse(req.url); |
| 115 | + |
| 116 | + if (parsed.auth && !parsed.protocol && ~parsed.href.indexOf('//')) { |
| 117 | + // This parses pathnames, and a strange pathname like //r@e should work |
| 118 | + parsed = url.parse(req.url.replace(/@/g, '%40')); |
| 119 | + } |
| 120 | + |
| 121 | + return parsed; |
| 122 | +}; |
0 commit comments