Add headers option to res.sendfile
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 3136e98 · Browse files · 3 files·+63 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+63 added−9 removed
- tests:+35 added−0 removed
- other:+28 added−9 removed
Provenance
- Commit
ce3d1fe- Tree
aade608- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
3136e98
3 files changed+63 added−9 removed
modified:lib/response.js+26 added-9 removed
276 unmodified lines277 * may have already been transferred.278 *279 * Options:280 *281- * - `maxAge` defaulting to 0282- * - `root` root directory for relative filenames283- * - `hidden` serve hidden files, defaulting to false281+ * - `maxAge` defaulting to 0 (can be string converted by `ms`)282+ * - `root` root directory for relative filenames283+ * - `headers` object of headers to serve with file284+ * - `hidden` serve hidden files, defaulting to false285 *286 * Other options are passed along to `send`.287 *288 * Examples:42 unmodified lines331 done = true;332 333 // clean up334 cleanup();334- if (!self.headersSent) self.removeHeader('Content-Disposition');335 336 // callback available337 if (fn) return fn(err);338 339- // list in limbo if there's no callback340- if (self.headersSent) return;341-339 // delegate340 next(err);341 }342 16 unmodified lines359 var file = send(req, path, options);360 file.on('error', error);361 file.on('directory', next);362 file.on('stream', stream);363+364+ if (options.headers) {365+ // set headers on successful transfer366+ file.on('headers', function headers(res) {367+ var obj = options.headers;368+ var keys = Object.keys(obj);369+370+ for (var i = 0; i < keys.length; i++) {371+ var k = keys[i];372+ res.setHeader(k, obj[k]);373+ }374+ });375+ }376+377+ // pipe378 file.pipe(this);379 this.on('finish', cleanup);380 };381 17 unmodified lines399 filename = null;400 }401 402 filename = filename || path;391- this.set('Content-Disposition', contentDisposition(filename));392- return this.sendfile(path, fn);403+404+ // set Content-Disposition when file is sent405+ var headers = {406+ 'Content-Disposition': contentDisposition(filename)407+ };408+409+ return this.sendfile(path, { headers: headers }, fn);410 };411 412 /**413 * Set _Content-Type_ response header with `type` through `mime.lookup()`382 unmodified linesmodified:test/res.sendfile.js+35 added
128 unmodified lines129 .get('/')130 .expect(200, 'tobi', done);131 })132 133+ it('should accept headers option', function(done){134+ var app = express();135+ var headers = {136+ 'x-success': 'sent',137+ 'x-other': 'done'138+ };139+140+ app.use(function(req, res){141+ res.sendfile('test/fixtures/user.html', { headers: headers });142+ });143+144+ request(app)145+ .get('/')146+ .expect('x-success', 'sent')147+ .expect('x-other', 'done')148+ .expect(200, done);149+ })150+151+ it('should ignore headers option on 404', function(done){152+ var app = express();153+ var headers = { 'x-success': 'sent' };154+155+ app.use(function(req, res){156+ res.sendfile('test/fixtures/user.nothing', { headers: headers });157+ });158+159+ request(app)160+ .get('/')161+ .expect(404, function (err, res) {162+ if (err) return done(err);163+ res.headers.should.not.have.property('x-success');164+ done();165+ });166+ })167+168 describe('with an absolute path', function(){169 it('should transfer the file', function(done){170 var app = express();171 126 unmodified linesmodified:History.md+2 added
2 unmodified lines3 4 * add deprecation message to non-plural `req.accepts*`5 * add deprecation message to `res.send(body, status)`6 * add deprecation message to `res.vary()`7+ * add `headers` option to `res.sendfile`8+ - use to set headers on successful file transfer9 * add `mergeParams` option to `Router`10 - merges `req.params` from parent routes11 * deprecate things with `depd` module12 * fix behavior when handling request without routes1711 unmodified lines