Skip to content
Myr

Merge branch 'downloads'

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 · parents f6ba793, b763a88 · Browse files · 4 files+202 added−12 removed
This commit

State

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

Diff stats

4 files+202 added−12 removed

  • tests:+157 added−0 removed
  • other:+45 added−12 removed

4 files+202 added−12 removed

modified:docs/guide.md+14 added-1 removed
82 unmodified lines
83 * _home_ Application base path used for _res.redirect()_ and transparently handling mounted apps.84 * _views_ Root views directory defaulting to **CWD/views**85 * _view engine_ Default view engine name for views rendered without extensions86 * _view options_ An object specifying global view options87 * _partials_ Root view partials directory defaulting to _views_/partials. 87 * _partials_ Root view partials directory defaulting to _views_/partials.88 * _stream threshold_ Bytesize indicating when a file should be streamed for _res.sendfile()_ using _fs.ReadStream()_ and _sys.pump()_.8990### Routing9192Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.
363 unmodified lines
456support HTTP caching, and does not perform any security checks. This method is utilized457by _res.download()_ to transfer static files, and allows you do to so from outside of458the public directory, so suitable security checks should be applied.459460This method accepts a callback which when given will be called on an exception, as well as when the transfer has completed. When a callback is not given, and the file has __not__ been streamed, _next(err)_ will be called on an exception.461462 res.sendfile(path, function(err, path){463 if (err) {464 // handle the error465 } else {466 console.log('transferred %s', path);467 }468 });469470When the filesize exceeds the _stream threshold_ (defaulting to 32k), the file will be streamed using _fs.ReadStream_ and _sys.pump()_.471472### res.download(file[, filename])473474Transfer the given _file_ as an attachment with optional alternative _filename_.475
279 unmodified lines
modified:lib/express/response.js+31 added-11 removed
10 unmodified lines
1112var fs = require('fs'),13 http = require('http'),14 path = require('path'),15 pump = require('sys').pump,16 utils = require('connect/utils'),17 mime = require('connect/utils').mime,18 Buffer = require('buffer').Buffer;19
101 unmodified lines
121 * The given callback `fn` is invoked when an error occurs,122 * passing it as the first argument, or when the file is transferred,123 * passing the path as the second argument.124 *125 * When the filesize is >= "stream threshold" (defaulting to 32k), the126 * file will be streamed using an `fs.ReadStream` and `sys.pump()`.127 *128 * @param {String} path129 * @param {Function} fn130 * @api public131 */132133http.ServerResponse.prototype.sendfile = function(path, fn){130 var self = this;131 fs.readFile(path, function(err, buf){132 if (err) {133 delete self.headers['Content-Disposition'];134 if (fn) {135 fn(err, path);136 } else {137 self.req.next(err);138 }134 var self = this,135 streamThreshold = this.app.set('stream threshold') || 32 * 1024;136137 function error(err) {138 delete self.headers['Content-Disposition'];139 if (fn) {140 fn(err, path);141 } else {142 self.req.next(err);143 }144 }145146 fs.stat(path, function(err, stat){147 if (err) return error(err);148 if (stat.size >= streamThreshold) {149 var stream = fs.createReadStream(path);150 self.contentType(path);151 self.header('Content-Length', stat.size);152 self.writeHead(200, self.headers);153 pump(stream, self, function(err){154 fn && fn(err, path);155 });156 } else {157 fs.readFile(path, function(err, buf){158 if (err) return error(err);159 self.contentType(path);141 self.send(buf);142 fn && fn(null, path);160 self.send(buf);161 fn && fn(null, path);162 });163 }164 });165};166
173 unmodified lines
added:test/fixtures/large.json+151 added
1{2 "foo":"bar",3 "foo":"bar",4 "foo":"bar",5 "foo":"bar",6 "foo":"bar",7 "foo":"bar",8 "foo":"bar",9 "foo":"bar",10 "foo":"bar",11 "foo":"bar",12 "foo":"bar",13 "foo":"bar",14 "foo":"bar",15 "foo":"bar",16 "foo":"bar",17 "foo":"bar",18 "foo":"bar",19 "foo":"bar",20 "foo":"bar",21 "foo":"bar",22 "foo":"bar",23 "foo":"bar",24 "foo":"bar",25 "foo":"bar",26 "foo":"bar",27 "foo":"bar",28 "foo":"bar",29 "foo":"bar",30 "foo":"bar",31 "foo":"bar",32 "foo":"bar",33 "foo":"bar",34 "foo":"bar",35 "foo":"bar",36 "foo":"bar",37 "foo":"bar",38 "foo":"bar",39 "foo":"bar",40 "foo":"bar",41 "foo":"bar",42 "foo":"bar",43 "foo":"bar",44 "foo":"bar",45 "foo":"bar",46 "foo":"bar",47 "foo":"bar",48 "foo":"bar",49 "foo":"bar",50 "foo":"bar",51 "foo":"bar",52 "foo":"bar",53 "foo":"bar",54 "foo":"bar",55 "foo":"bar",56 "foo":"bar",57 "foo":"bar",58 "foo":"bar",59 "foo":"bar",60 "foo":"bar",61 "foo":"bar",62 "foo":"bar",63 "foo":"bar",64 "foo":"bar",65 "foo":"bar",66 "foo":"bar",67 "foo":"bar",68 "foo":"bar",69 "foo":"bar",70 "foo":"bar",71 "foo":"bar",72 "foo":"bar",73 "foo":"bar",74 "foo":"bar",75 "foo":"bar",76 "foo":"bar",77 "foo":"bar",78 "foo":"bar",79 "foo":"bar",80 "foo":"bar",81 "foo":"bar",82 "foo":"bar",83 "foo":"bar",84 "foo":"bar",85 "foo":"bar",86 "foo":"bar",87 "foo":"bar",88 "foo":"bar",89 "foo":"bar",90 "foo":"bar",91 "foo":"bar",92 "foo":"bar",93 "foo":"bar",94 "foo":"bar",95 "foo":"bar",96 "foo":"bar",97 "foo":"bar",98 "foo":"bar",99 "foo":"bar",100 "foo":"bar",101 "foo":"bar",102 "foo":"bar",103 "foo":"bar",104 "foo":"bar",105 "foo":"bar",106 "foo":"bar",107 "foo":"bar",108 "foo":"bar",109 "foo":"bar",110 "foo":"bar",111 "foo":"bar",112 "foo":"bar",113 "foo":"bar",114 "foo":"bar",115 "foo":"bar",116 "foo":"bar",117 "foo":"bar",118 "foo":"bar",119 "foo":"bar",120 "foo":"bar",121 "foo":"bar",122 "foo":"bar",123 "foo":"bar",124 "foo":"bar",125 "foo":"bar",126 "foo":"bar",127 "foo":"bar",128 "foo":"bar",129 "foo":"bar",130 "foo":"bar",131 "foo":"bar",132 "foo":"bar",133 "foo":"bar",134 "foo":"bar",135 "foo":"bar",136 "foo":"bar",137 "foo":"bar",138 "foo":"bar",139 "foo":"bar",140 "foo":"bar",141 "foo":"bar",142 "foo":"bar",143 "foo":"bar",144 "foo":"bar",145 "foo":"bar",146 "foo":"bar",147 "foo":"bar",148 "foo":"bar",149 "foo":"bar",150 "foo":"bar"151}
modified:test/response.test.js+6 added
183 unmodified lines
184 185 'test #sendfile()': function(assert){186 var app = express.createServer();187188 app.set('stream threshold', 2 * 1024)189190 app.get('/*', function(req, res, next){191 var file = req.params[0],192 filePath = __dirname + '/fixtures/' + file;193 res.sendfile(filePath, function(err, path){
15 unmodified lines
209 { body: 'Internal Server Error', status: 500 });210 assert.response(app,211 { url: '/partials' },212 { body: 'Internal Server Error', status: 500 });213 assert.response(app,214 { url: '/large.json' },215 { status: 200, headers: { 'Content-Length': 2535, 'Content-Type': 'application/json' }});216 },217 218 'test #download()': function(assert){219 var app = express.createServer();
23 unmodified lines
243 assert.response(app,244 { url: '/json' },245 { body: '{"name":"tj"}', status: 200, headers: {246 'Content-Type': 'application/json',247 'Content-Length': '13',248 'Content-Disposition': 'attachment; filename="account.json"'249 }});250 251 assert.response(app,
52 unmodified lines