Skip to content
Myr

change res.sendfile() to use send() module

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 7a5041b · Browse files · 9 files+108 added−78 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+108 added−78 removed

  • tests:+45 added−6 removed
  • other:+63 added−72 removed

9 files+108 added−78 removed

modified:examples/auth/app.js+10 added-5 removed
modified:examples/expose-data-to-client/index.js-8 removed
modified:examples/mvc/index.js+18 added-17 removed
23 unmodified lines
24 sess.messages.push(msg);25 return this;26};2728// log29if (!module.parent) app.use(express.logger('dev'));3031// serve static files32app.use(express.static(__dirname + '/public'));3334// session support35app.use(express.cookieParser('some secret here'));36app.use(express.session());3738// parse request bodies (req.body)39app.use(express.bodyParser());4041// support _method (PUT in forms etc)42app.use(express.methodOverride());4344// expose the "messages" local variable when views are rendered29app.locals.use(function(req, res){45app.use(function(req, res, next){46 var msgs = req.session.messages || [];4748 // expose "messages" local variable49 res.locals.messages = msgs;
10 unmodified lines
6061 // empty or "flush" the messages so they62 // don't build up63 req.session.messages = [];64 next();65});6650// log51if (!module.parent) app.use(express.logger('dev'));5253// serve static files54app.use(express.static(__dirname + '/public'));5556// session support57app.use(express.cookieParser('some secret here'));58app.use(express.session());5960// parse request bodies (req.body)61app.use(express.bodyParser());6263// support _method (PUT in forms etc)64app.use(express.methodOverride());6567// load controllers68require('./lib/boot')(app, { verbose: !module.parent });6970// assume "not found" in the error msgs
23 unmodified lines
modified:examples/view-locals/index.js-23 removed
86 unmodified lines
8788function users2(req, res, next) {89 User.all(function(err, users){90 if (err) return next(err);91 // this would not be ideal for *this*91 res.locals.users = users.filter(ferrets);92 next();93 })94}
6 unmodified lines
101 res.render('user', { title: 'Users' });102});103104106107108// let's assume we wanted to load the users109// and count for every res.render() call, we110// could use app.locals.use() for this. These111// are callbacks which run in parallel ONLY112// when res.render() is invoked. If no views113// are rendered, there is no overhead.114115// This may be ideal if you want to load auxiliary116// user information, but only for templates. Note117// that (req, res) are available to you, so you may118// access req.session.user etc.119120// Keep in mind these execute in *parallel*, so these121// callbacks should not depend on each other, this122// also makes them slightly more efficient than123// using middleware which execute sequentially124125app.locals.use(count2);126app.locals.use(users2);127105app.get('/locals', function(req, res){106 res.render('user', { title: 'Users' });107});108
2 unmodified lines
modified:lib/middleware.js+1 added-1 removed
24 unmodified lines
2526 req.__proto__ = app.request;27 res.__proto__ = app.response;2829 res.locals = utils.locals(res);29 res.locals = res.locals || utils.locals(res);3031 next();32 }33};
modified:lib/response.js+33 added-18 removed
10 unmodified lines
11 , normalizeTypes = require('./utils').normalizeTypes12 , statusCodes = http.STATUS_CODES13 , send = connect.static.send14 , cookie = require('cookie')15 , send = require('send')16 , mime = connect.mime17 , basename = path.basename18 , extname = path.extname19 , join = path.join;
151 unmodified lines
171res.sendfile = function(path, options, fn){172 var self = this173 , req = self.req174 , next = this.req.next174 , options = options || {};175 , options = options || {}176 , done;177178 // support function as second arg179 if ('function' == typeof options) {180 fn = options;181 options = {};182 }183182 // callback183 options.callback = function(err){184 if (err) {185 // cast ENOENT186 if ('ENOENT' == err.code) err = utils.error(404);184 // socket errors185 req.socket.on('error', error);186188 // ditch content-disposition to prevent funky responses189 if (!self.headerSent) self.removeHeader('Content-Disposition');187 // errors188 function error(err) {189 if (done) return;190 done = true;191191 // woot! callback available192 if (fn) return fn(err);192 // clean up193 req.socket.removeListener('error', error);194 if (!self.headerSent) self.removeHeader('Content-Disposition');195194 // lost in limbo if there's no callback195 if (self.headerSent) return;196 // callback available197 if (fn) return fn(err);198197 return req.next(err);198 }199 // list in limbo if there's no callback200 if (self.headerSent) return;201200 fn && fn();201 };202 // delegate203 next(err);204 }205206 // streaming207 function stream() {208 if (done) return;209 req.socket.removeListener('error', error);210 if (fn) self.on('finish', fn);211 }212213 // transfer204 options.path = encodeURIComponent(path);205 send(this.req, this, next, options);214 var file = send(req, path);215 if (options.root) file.root(options.root);216 file.maxage(options.maxAge || 0);217 file.on('error', error);218 file.on('directory', next);219 file.on('stream', stream);220 file.pipe(this);221};222223/**224 * Transfer the file at the given `path` as an attachment.
378 unmodified lines
modified:test/res.locals.js+22 added-3 removed
15 unmodified lines
16 });1718 request(app)19 .get('/')20 .end(function(res){21 done();22 })20 .expect(200, done);21 })22 })2324 it('should work when mounted', function(done){25 var app = express();26 var blog = express();2728 app.use(blog);2930 blog.use(function(req, res, next){31 res.locals.foo = 'bar';32 next();33 });3435 app.use(function(req, res){36 res.locals.foo.should.equal('bar');37 res.end();38 });3940 request(app)41 .get('/')42 .expect(200, done);43 })44})
modified:test/res.sendfile.js+23 added-3 removed
9 unmodified lines
1011 app.use(function(req, res){12 res.sendfile('test/fixtures/user.html', function(err){13 assert(!err);14 req.socket.listeners('error').should.have.length(1); // node's original handler15 done();16 });17 });18
21 unmodified lines
40 , calls = 0;4142 app.use(function(req, res){43 res.sendfile('test/fixtures/nope.html', function(err){43 assert(!res.headerSent);44 ++calls;45 assert(!res.headerSent);46 res.send(err.message);47 });48 });4950 request(app)51 .get('/')52 .end(function(err, res){52 calls.should.equal(1);53 res.text.should.equal('Not Found');53 assert(1 == calls, 'called too many times');54 res.text.should.equal("ENOENT, stat 'test/fixtures/nope.html'");55 res.statusCode.should.equal(200);56 done();57 });58 })
28 unmodified lines
87 .get('/')88 .expect('Forbidden')89 .expect(200, done);90 })9192 it('should invoke the callback on socket error', function(done){93 var app = express()94 , calls = 0;9596 app.use(function(req, res){97 res.sendfile('test/fixtures/user.html', function(err){98 assert(!res.headerSent);99 req.socket.listeners('error').should.have.length(1); // node's original handler100 done();101 });102103 req.socket.emit('error', new Error('broken!'));104 });105106 request(app)107 .get('/')108 .end(function(){});109 })110 })111112 describe('.sendfile(path)', function(){113 describe('with an absolute path', function(){
129 unmodified lines
modified:package.json+1 added
16 unmodified lines
17 "mkdirp": "0.3.3",18 "cookie": "0.0.3",19 "fresh": "0.1.0",20 "methods": "0.0.1",21 "send": "0.0.2",22 "debug": "*"23 },24 "devDependencies": {25 "ejs": "*",
21 unmodified lines