diff --git a/README.md b/README.md index 4167926..6339acf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -229,7 +230,7 @@ var app = http.createServer(function onRequest (req, res) { }).listen(3000) ``` -## License +## License [MIT](LICENSE) diff --git a/index.js b/index.js index 81ec0b3..3982980 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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) diff --git a/package.json b/package.json index 3a93e89..c34ce04 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/send.js b/test/send.js index da101aa..173ffd3 100644 --- a/test/send.js +++ b/test/send.js @@ -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 = '

tobi

' + + 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/')