Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ This is skipped if the requested file already has an extension.

##### index

By default send supports "index.html" files, to disable this
set `false` or to supply a new index pass a string or an array
in preferred order.
By default send supports `index.html` files, to disable this set `false`. You
can supply a different index file by setting it to a string or array of strings
in the preferred order to be evaluated, or you can set it to a function that
will process the index content response.

##### lastModified

Expand Down Expand Up @@ -229,7 +230,7 @@ var app = http.createServer(function onRequest (req, res) {
}).listen(3000)
```

## License
## License

[MIT](LICENSE)

Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,18 @@ function SendStream (req, path, options) {
? normalizeList(opts.extensions, 'extensions option')
: []

this._index = opts.index !== undefined
? normalizeList(opts.index, 'index option')
: ['index.html']
switch (typeof opts.index) {
case 'function':
this._index = opts.index
break

case 'undefined':
this._index = ['index.html']
break

default:
this._index = normalizeList(opts.index, 'index option')
}

this._lastModified = opts.lastModified !== undefined
? Boolean(opts.lastModified)
Expand Down Expand Up @@ -707,6 +716,8 @@ SendStream.prototype.sendIndex = function sendIndex (path) {
var self = this

function next (err) {
if (typeof self._index === 'function') return self._index(path)

if (++i >= self._index.length) {
if (err) return self.onStatError(err)
return self.error(404)
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
"statuses": "~1.3.0"
},
"devDependencies": {
"after": "0.8.1",
"eslint": "2.11.1",
"eslint-config-standard": "5.3.1",
"eslint-plugin-promise": "1.3.1",
"eslint-plugin-standard": "1.3.2",
"istanbul": "0.4.3",
"mocha": "2.5.3",
"supertest": "1.1.0"
"after": "0.8.2",
"eslint": "3.4.0",
"eslint-config-standard": "6.0.0",
"eslint-plugin-promise": "2.0.1",
"eslint-plugin-standard": "2.0.0",
"istanbul": "0.4.5",
"mocha": "3.0.2",
"supertest": "2.0.0"
},
"files": [
"HISTORY.md",
Expand Down
12 changes: 12 additions & 0 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,18 @@ describe('send(file, options)', function () {
.expect(200, fs.readFileSync(path.join(fixtures, 'pets', 'index.html'), 'utf8'), done)
})

it('should accept a function', function (done) {
var expected = '<p>tobi</p>'

function index (path) {
this.res.end(expected)
}

request(createServer({root: fixtures, index: index}))
.get('/pets/')
.expect(200, expected, done)
})

it('should 404 if no index file found (file)', function (done) {
request(createServer({root: fixtures, index: 'default.htm'}))
.get('/pets/')
Expand Down