-
-
Notifications
You must be signed in to change notification settings - Fork 194
index event
#89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
index event
#89
Changes from all commits
fb8c0e7
90e8f98
fb27471
77bd35d
b1491ca
1e688d6
f4c1476
6d2600c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,9 +81,14 @@ 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 index option | ||
| to `false`, or you can be able to supply a new index passing a string with the | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "you can be able to" doesn't sound right. I think probably just remove those words?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And passing would become just pass
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's formal english... As you want.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I can't seem to figure out what your response means... |
||
| filename or an array in the preferred order. | ||
|
|
||
| Alternatively, you can pass a function that will generate the index. The | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than tacking this on as a separate paragraph, can you incorporate them all together? Otherwise, it sounds very hack in, but having a complete paragraph saying that the option only takes a boolean, string, or array, then another paragraph following to say "but wait! We forgot to add it takes a function too" |
||
| function will receive the filesystem absolute path and will have the | ||
| `SendStream` instance as `this` object. You should call internally to the | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having the "this" populated on a callback like function is very odd, and very easy to get lost. It is kind of annoying already for events, but typically I never see this on straight functions. Can everything a user would need just be passed as arguments? |
||
| `this.send()` method or try to mimic its behaviour. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the docs be clearer on how someone is supposed to "mimic its behavior"? |
||
|
|
||
| ##### lastModified | ||
|
|
||
|
|
@@ -229,7 +234,7 @@ var app = http.createServer(function onRequest (req, res) { | |
| }).listen(3000) | ||
| ``` | ||
|
|
||
| ## License | ||
| ## License | ||
|
|
||
| [MIT](LICENSE) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,13 +142,20 @@ function SendStream (req, path, options) { | |
| this._dotfiles = undefined | ||
| } | ||
|
|
||
| this._extensions = opts.extensions !== undefined | ||
| ? normalizeList(opts.extensions, 'extensions option') | ||
| : [] | ||
| this._extensions = normalizeList(opts.extensions, 'extensions option') | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a necessary change for this PR?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any changes to normalizeList in your PR. Perhaps I should be clearer: don't include changes in your PR that are not accomplishing your goal. These types of changes need to be different PRs. Please revert this change. |
||
| 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) | ||
|
|
@@ -542,11 +549,16 @@ SendStream.prototype.pipe = function pipe (res) { | |
| } | ||
|
|
||
| // index file support | ||
| if (typeof this._index === 'function') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this check just be moved into the sendIndex method? It is a public method, and it's weird the event emit is in there but not this check.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would in fact remove the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I don't understand your response. Can you say it in a different way? |
||
| this._index(path) | ||
| return res | ||
| } | ||
| if (this._index.length && this.path[this.path.length - 1] === '/') { | ||
| this.sendIndex(path) | ||
| return res | ||
| } | ||
|
|
||
| // Render a file, or an index as a file | ||
| this.sendFile(path) | ||
| return res | ||
| } | ||
|
|
@@ -703,6 +715,11 @@ SendStream.prototype.sendFile = function sendFile (path) { | |
| * @api private | ||
| */ | ||
| SendStream.prototype.sendIndex = function sendIndex (path) { | ||
| if (listenerCount(this, 'index')) { | ||
| this.emit('index', path) | ||
| return | ||
| } | ||
|
|
||
| var i = -1 | ||
| var self = this | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -702,9 +702,11 @@ describe('send(file).pipe(res)', function () { | |
| .pipe(res) | ||
| }) | ||
|
|
||
| var filePath = path.join(fixtures, 'pets', 'index.html') | ||
|
|
||
| request(app) | ||
| .get('/pets/') | ||
| .expect(200, fs.readFileSync(path.join(fixtures, 'pets', 'index.html'), 'utf8'), done) | ||
| .expect(200, fs.readFileSync(filePath, 'utf8'), done) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a necessary change for this PR?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ESlint was giving me errors of lines too long, this fixed it. Source code lines should never be longer than 80 columns.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert the change if you would like this landed... Please don't include unrelated changes. If we wanted to enforce a max line length, we would do so, but we do not. |
||
| }) | ||
| }) | ||
|
|
||
|
|
@@ -1115,6 +1117,41 @@ describe('send(file, options)', function () { | |
| .expect(200, '<p>tobi</p>', done) | ||
| }) | ||
|
|
||
| it('should support function', function (done) { | ||
| function index (path) { | ||
| var res = this.res | ||
|
|
||
| res.end('<p>tobi</p>') | ||
| } | ||
|
|
||
| var app = http.createServer(function (req, res) { | ||
| send(req, req.url, {root: fixtures}) | ||
| .on('index', index) | ||
| .pipe(res) | ||
| }) | ||
|
|
||
| request(app) | ||
| .get('/') | ||
| .expect(200, '<p>tobi</p>', done) | ||
| }) | ||
|
|
||
| it('should support function as option', function (done) { | ||
| function index (path) { | ||
| var res = this.res | ||
|
|
||
| res.end('<p>tobi</p>') | ||
| } | ||
|
|
||
| var app = http.createServer(function (req, res) { | ||
| send(req, req.url, {root: fixtures, index: index}) | ||
| .pipe(res) | ||
| }) | ||
|
|
||
| request(app) | ||
| .get('/') | ||
| .expect(200, '<p>tobi</p>', done) | ||
| }) | ||
|
|
||
| it('should support disabling', function (done) { | ||
| request(createServer({root: fixtures, index: false})) | ||
| .get('/pets/') | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this section is about the index option, I don't think you need to refer to itself, as it ends up being really wordy.