Skip to content
Myr

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

3 files+63 added−9 removed

modified:lib/response.js+26 added-9 removed
276 unmodified lines
277 * 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 lines
331 done = true;332333 // clean up334 cleanup();334 if (!self.headersSent) self.removeHeader('Content-Disposition');335336 // callback available337 if (fn) return fn(err);338339 // list in limbo if there's no callback340 if (self.headersSent) return;341339 // delegate340 next(err);341 }342
16 unmodified lines
359 var file = send(req, path, options);360 file.on('error', error);361 file.on('directory', next);362 file.on('stream', stream);363364 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);369370 for (var i = 0; i < keys.length; i++) {371 var k = keys[i];372 res.setHeader(k, obj[k]);373 }374 });375 }376377 // pipe378 file.pipe(this);379 this.on('finish', cleanup);380};381
17 unmodified lines
399 filename = null;400 }401402 filename = filename || path;391 this.set('Content-Disposition', contentDisposition(filename));392 return this.sendfile(path, fn);403404 // set Content-Disposition when file is sent405 var headers = {406 'Content-Disposition': contentDisposition(filename)407 };408409 return this.sendfile(path, { headers: headers }, fn);410};411412/**413 * Set _Content-Type_ response header with `type` through `mime.lookup()`
382 unmodified lines
modified:test/res.sendfile.js+35 added
128 unmodified lines
129 .get('/')130 .expect(200, 'tobi', done);131 })132133 it('should accept headers option', function(done){134 var app = express();135 var headers = {136 'x-success': 'sent',137 'x-other': 'done'138 };139140 app.use(function(req, res){141 res.sendfile('test/fixtures/user.html', { headers: headers });142 });143144 request(app)145 .get('/')146 .expect('x-success', 'sent')147 .expect('x-other', 'done')148 .expect(200, done);149 })150151 it('should ignore headers option on 404', function(done){152 var app = express();153 var headers = { 'x-success': 'sent' };154155 app.use(function(req, res){156 res.sendfile('test/fixtures/user.nothing', { headers: headers });157 });158159 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 })167168 describe('with an absolute path', function(){169 it('should transfer the file', function(done){170 var app = express();171
126 unmodified lines
modified:History.md+2 added
2 unmodified lines
34 * 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 routes
1711 unmodified lines