@@ -38,14 +38,16 @@ var url = require('url');
3838 * - `redirect` Redirect to trailing "/" when the pathname is a dir. defaults to true
3939 * - `index` Default file name, defaults to 'index.html'
4040 *
41+ * Further options are forwarded on to `send`.
42+ *
4143 * @param {String } root
4244 * @param {Object } options
4345 * @return {Function }
4446 * @api public
4547 */
4648
4749exports = module . exports = function ( root , options ) {
48- options = options || { } ;
50+ options = extend ( { } , options ) ;
4951
5052 // root required
5153 if ( ! root ) throw new TypeError ( 'root path required' ) ;
@@ -56,8 +58,13 @@ exports = module.exports = function(root, options){
5658 // default redirect
5759 var redirect = false !== options . redirect ;
5860
61+ // setup options for send
62+ options . maxage = options . maxage || options . maxAge || 0 ;
63+ options . root = root ;
64+
5965 return function staticMiddleware ( req , res , next ) {
6066 if ( 'GET' != req . method && 'HEAD' != req . method ) return next ( ) ;
67+ var opts = extend ( { } , options ) ;
6168 var originalUrl = url . parse ( req . originalUrl || req . url ) ;
6269 var path = parseurl ( req ) . pathname ;
6370
@@ -80,11 +87,7 @@ exports = module.exports = function(root, options){
8087 next ( err ) ;
8188 }
8289
83- send ( req , path )
84- . maxage ( options . maxAge || 0 )
85- . root ( root )
86- . index ( options . index || 'index.html' )
87- . hidden ( options . hidden )
90+ send ( req , path , opts )
8891 . on ( 'error' , error )
8992 . on ( 'directory' , directory )
9093 . pipe ( res ) ;
@@ -115,3 +118,22 @@ function escape(html) {
115118 . replace ( / > / g, '>' )
116119 . replace ( / " / g, '"' ) ;
117120} ;
121+
122+ /**
123+ * Shallow clone a single object.
124+ *
125+ * @param {Object } obj
126+ * @param {Object } source
127+ * @return {Object }
128+ * @api private
129+ */
130+
131+ function extend ( obj , source ) {
132+ if ( ! source ) return obj ;
133+
134+ for ( var prop in source ) {
135+ obj [ prop ] = source [ prop ] ;
136+ }
137+
138+ return obj ;
139+ } ;
0 commit comments