Remove 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 ccdbe4e · Browse files · 3 files·+2 added−450 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+2 added−450 removed
- tests:+0 added−377 removed
- other:+2 added−73 removed
Provenance
- Commit
0fc4f07- Tree
0f7c670- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
ccdbe4e
3 files changed+2 added−450 removed
modified:lib/response.js+1 added-73 removed
386 unmodified lines387 });388 };389 390 /**391- * Transfer the file at the given `path`.392- *393- * Automatically sets the _Content-Type_ response header field.394- * The callback `fn(err)` is invoked when the transfer is complete395- * or when an error occurs. Be sure to check `res.sentHeader`396- * if you wish to attempt responding, as the header and some data397- * may have already been transferred.398- *399- * Options:400- *401- * - `maxAge` defaulting to 0 (can be string converted by `ms`)402- * - `root` root directory for relative filenames403- * - `headers` object of headers to serve with file404- * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them405- *406- * Other options are passed along to `send`.407- *408- * Examples:409- *410- * The following example illustrates how `res.sendfile()` may411- * be used as an alternative for the `static()` middleware for412- * dynamic situations. The code backing `res.sendfile()` is actually413- * the same code, so HTTP cache support etc is identical.414- *415- * app.get('/user/:uid/photos/:file', function(req, res){416- * var uid = req.params.uid417- * , file = req.params.file;418- *419- * req.user.mayViewFilesFrom(uid, function(yes){420- * if (yes) {421- * res.sendfile('/uploads/' + uid + '/' + file);422- * } else {423- * res.send(403, 'Sorry! you cant see that.');424- * }425- * });426- * });427- *428- * @api public429- */430-431-res.sendfile = function(path, options, fn){432- var req = this.req;433- var res = this;434- var next = req.next;435-436- // support function as second arg437- if (typeof options === 'function') {438- fn = options;439- options = {};440- }441-442- options = options || {};443-444- // create file stream445- var file = send(req, path, options);446-447- // transfer448- sendfile(res, file, options, function (err) {449- if (fn) return fn(err);450- if (err && err.code === 'EISDIR') return next();451-452- // next() all but aborted errors453- if (err && err.code !== 'ECONNABORT') {454- next(err);455- }456- });457-};458-459-res.sendfile = deprecate.function(res.sendfile,460- 'res.sendfile: Use res.sendFile instead');461-462-/**391 * Transfer the file at the given `path` as an attachment.392 *393 * Optionally providing an alternate attachment `filename`,394 * and optional callback `fn(err)`. The callback is invoked395 * when the data transfer is complete, or when an error has396 * ocurred. Be sure to check `res.headersSent` if you plan to respond.397 *470- * This method uses `res.sendfile()`.398+ * This method uses `res.sendFile()`.399 *400 * @api public401 */402 479 unmodified linesmodified:test/res.sendFile.js-377 removed
205 unmodified lines206 .get('/')207 .expect(200, 'got it', done);208 })209 })210-211- describe('.sendfile(path, fn)', function(){212- it('should invoke the callback when complete', function(done){213- var app = express();214- var cb = after(2, done);215-216- app.use(function(req, res){217- res.sendfile('test/fixtures/user.html', cb)218- });219-220- request(app)221- .get('/')222- .expect(200, cb);223- })224-225- it('should utilize the same options as express.static()', function(done){226- var app = express();227-228- app.use(function(req, res){229- res.sendfile('test/fixtures/user.html', { maxAge: 60000 });230- });231-232- request(app)233- .get('/')234- .expect('Cache-Control', 'public, max-age=60')235- .end(done);236- })237-238- it('should invoke the callback when client aborts', function (done) {239- var cb = after(1, done);240- var app = express();241-242- app.use(function (req, res) {243- setImmediate(function () {244- res.sendfile('test/fixtures/name.txt', function (err) {245- should(err).be.ok;246- err.code.should.equal('ECONNABORT');247- cb();248- });249- });250- test.abort();251- });252-253- var test = request(app).get('/');254- test.expect(200, cb);255- })256-257- it('should invoke the callback when client already aborted', function (done) {258- var cb = after(1, done);259- var app = express();260-261- app.use(function (req, res) {262- onFinished(res, function () {263- res.sendfile('test/fixtures/name.txt', function (err) {264- should(err).be.ok;265- err.code.should.equal('ECONNABORT');266- cb();267- });268- });269- test.abort();270- });271-272- var test = request(app).get('/');273- test.expect(200, cb);274- })275-276- it('should invoke the callback on 404', function(done){277- var app = express()278- , calls = 0;279-280- app.use(function(req, res){281- res.sendfile('test/fixtures/nope.html', function(err){282- ++calls;283- assert(!res.headersSent);284- res.send(err.message);285- });286- });287-288- request(app)289- .get('/')290- .end(function(err, res){291- assert(1 == calls, 'called too many times');292- res.text.should.startWith("ENOENT, stat");293- res.statusCode.should.equal(200);294- done();295- });296- })297-298- it('should not override manual content-types', function(done){299- var app = express();300-301- app.use(function(req, res){302- res.contentType('txt');303- res.sendfile('test/fixtures/user.html');304- });305-306- request(app)307- .get('/')308- .expect('Content-Type', 'text/plain; charset=utf-8')309- .end(done);310- })311-312- it('should invoke the callback on 403', function(done){313- var app = express()314- , calls = 0;315-316- app.use(function(req, res){317- res.sendfile('test/fixtures/foo/../user.html', function(err){318- assert(!res.headersSent);319- ++calls;320- res.send(err.message);321- });322- });323-324- request(app)325- .get('/')326- .expect('Forbidden')327- .expect(200, done);328- })329-330- it('should invoke the callback on socket error', function(done){331- var app = express()332- , calls = 0;333-334- app.use(function(req, res){335- res.sendfile('test/fixtures/user.html', function(err){336- assert(!res.headersSent);337- req.socket.listeners('error').should.have.length(1); // node's original handler338- done();339- });340-341- req.socket.emit('error', new Error('broken!'));342- });343-344- request(app)345- .get('/')346- .end(function(){});347- })348- })349-350- describe('.sendfile(path)', function(){351- it('should not serve dotfiles', function(done){352- var app = express();353-354- app.use(function(req, res){355- res.sendfile('test/fixtures/.name');356- });357-358- request(app)359- .get('/')360- .expect(404, done);361- })362-363- it('should accept dotfiles option', function(done){364- var app = express();365-366- app.use(function(req, res){367- res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });368- });369-370- request(app)371- .get('/')372- .expect(200, 'tobi', done);373- })374-375- it('should accept headers option', function(done){376- var app = express();377- var headers = {378- 'x-success': 'sent',379- 'x-other': 'done'380- };381-382- app.use(function(req, res){383- res.sendfile('test/fixtures/user.html', { headers: headers });384- });385-386- request(app)387- .get('/')388- .expect('x-success', 'sent')389- .expect('x-other', 'done')390- .expect(200, done);391- })392-393- it('should ignore headers option on 404', function(done){394- var app = express();395- var headers = { 'x-success': 'sent' };396-397- app.use(function(req, res){398- res.sendfile('test/fixtures/user.nothing', { headers: headers });399- });400-401- request(app)402- .get('/')403- .expect(404, function (err, res) {404- if (err) return done(err);405- res.headers.should.not.have.property('x-success');406- done();407- });408- })409-410- it('should transfer a file', function (done) {411- var app = express();412-413- app.use(function (req, res) {414- res.sendfile('test/fixtures/name.txt');415- });416-417- request(app)418- .get('/')419- .expect(200, 'tobi', done);420- });421-422- it('should transfer a directory index file', function (done) {423- var app = express();424-425- app.use(function (req, res) {426- res.sendfile('test/fixtures/blog/');427- });428-429- request(app)430- .get('/')431- .expect(200, '<b>index</b>', done);432- });433-434- it('should 404 for directory without trailing slash', function (done) {435- var app = express();436-437- app.use(function (req, res) {438- res.sendfile('test/fixtures/blog');439- });440-441- request(app)442- .get('/')443- .expect(404, done);444- });445-446- it('should transfer a file with urlencoded name', function (done) {447- var app = express();448-449- app.use(function (req, res) {450- res.sendfile('test/fixtures/%25%20of%20dogs.txt');451- });452-453- request(app)454- .get('/')455- .expect(200, '20%', done);456- });457-458- describe('with an absolute path', function(){459- it('should transfer the file', function(done){460- var app = express();461-462- app.use(function(req, res){463- res.sendfile(__dirname + '/fixtures/user.html');464- });465-466- request(app)467- .get('/')468- .end(function(err, res){469- res.text.should.equal('<p>{{user.name}}</p>');470- res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');471- done();472- });473- })474- })475-476- describe('with a relative path', function(){477- it('should transfer the file', function(done){478- var app = express();479-480- app.use(function(req, res){481- res.sendfile('test/fixtures/user.html');482- });483-484- request(app)485- .get('/')486- .end(function(err, res){487- res.text.should.equal('<p>{{user.name}}</p>');488- res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');489- done();490- });491- })492-493- it('should serve relative to "root"', function(done){494- var app = express();495-496- app.use(function(req, res){497- res.sendfile('user.html', { root: 'test/fixtures/' });498- });499-500- request(app)501- .get('/')502- .end(function(err, res){503- res.text.should.equal('<p>{{user.name}}</p>');504- res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');505- done();506- });507- })508-509- it('should consider ../ malicious when "root" is not set', function(done){510- var app = express();511-512- app.use(function(req, res){513- res.sendfile('test/fixtures/foo/../user.html');514- });515-516- request(app)517- .get('/')518- .expect(403, done);519- })520-521- it('should allow ../ when "root" is set', function(done){522- var app = express();523-524- app.use(function(req, res){525- res.sendfile('foo/../user.html', { root: 'test/fixtures' });526- });527-528- request(app)529- .get('/')530- .expect(200, done);531- })532-533- it('should disallow requesting out of "root"', function(done){534- var app = express();535-536- app.use(function(req, res){537- res.sendfile('foo/../../user.html', { root: 'test/fixtures' });538- });539-540- request(app)541- .get('/')542- .expect(403, done);543- })544-545- it('should next(404) when not found', function(done){546- var app = express()547- , calls = 0;548-549- app.use(function(req, res){550- res.sendfile('user.html');551- });552-553- app.use(function(req, res){554- assert(0, 'this should not be called');555- });556-557- app.use(function(err, req, res, next){558- ++calls;559- next(err);560- });561-562- request(app)563- .get('/')564- .end(function(err, res){565- res.statusCode.should.equal(404);566- calls.should.equal(1);567- done();568- });569- })570-571- describe('with non-GET', function(){572- it('should still serve', function(done){573- var app = express()574- , calls = 0;575-576- app.use(function(req, res){577- res.sendfile(__dirname + '/fixtures/name.txt');578- });579-580- request(app)581- .get('/')582- .expect('tobi', done);583- })584- })585- })586- })210 })211 212 function createApp(path, options, fn) {213 var app = express();7 unmodified linesmodified:History.md+1 added
7 unmodified lines8 - `req.acceptsLanguage` - use `req.acceptsLanguages`9 - `res.json(obj, status)` signature - use `res.json(status, obj)`10 - `res.jsonp(obj, status)` signature - use `res.jsonp(status, obj)`11 - `res.send(body, status)` signature - use `res.send(status, body)`12+ - `res.sendfile` - use `res.sendFile` instead13 - `express.query` middleware14 * change:15 - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname16 - `req.query` is now a getter instead of a plain property2453 unmodified lines