Skip to content

Commit

Permalink
refactored res.sendfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 18, 2011
1 parent 0932bde commit 5da28be
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ res.status = function(code){
};

/**
* Transfer the file at the given `path`. Automatically sets
* the _Content-Type_ response header field. `next()` is called
* when `path` is a directory, or when an error occurs.
* Transfer the file at the given `path`.
*
* Automatically sets the _Content-Type_ response header field.
* The callback `fn(err)` is invoked when the transfer is complete
* or when an error occurs. Be sure to check `res.headerSent`
* if you wish to attempt responding, as the header and some data
* may have already been transferred.
*
* Options:
*
Expand All @@ -159,17 +163,25 @@ res.status = function(code){
*/

res.sendfile = function(path, options, fn){
var next = this.req.next;
options = options || {};
var self = this
, next = this.req.next
, options = options || {};

// support function as second arg
if ('function' == typeof options) {
fn = options;
options = {};
}

// callback
options.callback = function(err){
// TODO: remove when node adds this
self.headerSent = !! self._header;
fn && fn(err);
};

// transfer
options.path = encodeURIComponent(path);
options.callback = fn;
send(this.req, this, next, options);
};

Expand Down

0 comments on commit 5da28be

Please sign in to comment.