Skip to content

Commit 8e175f2

Browse files
committed
Accept options directly to send module
1 parent e7d2ba1 commit 8e175f2

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
HEAD
22
====
33

4+
* Accept options directly to `send` module
45
56

67
1.0.4 / 2014-04-07

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var serveStatic = require('serve-static');
1212

1313
var app = connect();
1414

15-
app.use(serveStatic('public/ftp', {'index': 'default.html'}));
15+
app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}));
1616
app.listen();
1717
```
1818

index.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4749
exports = 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

Comments
 (0)