fixed downloads example
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 722a92f · Browse files · 2 files·+15 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+15 added−22 removed
- other:+15 added−22 removed
Provenance
- Commit
be52f38- Tree
820a3bc- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
722a92f
2 files changed+15 added−22 removed
modified:examples/downloads/app.js+12 added-21 removed
1234566788991011124 unmodified lines1718192022232425262728292131323334353637382223242526272829423031323334353645373839402 unmodified lines
/** * Module dependencies. */
var express = require('../../lib/express');var express = require('../../') , app = express();
var app = express.createServer();
app.get('/', function(req, res){ res.send('<ul>' + '<li>Download <a href="/files/amazing.txt">amazing.txt</a>.</li>' + '<li>Download <a href="/files/missing.txt">missing.txt</a>.</li>'4 unmodified lines// but here we name it :fileapp.get('/files/:file(*)', function(req, res, next){ var file = req.params.file , path = __dirname + '/files/' + file; // either res.download(path) and let // express handle failures, or provide // a callback as shown below res.download(path, function(err){ // if an error occurs in this callback // the file most likely does not exist, // and it's safe to respond or next(err) if (err) return next(err);
// the file has been transferred, do not respond // from here, though you may use this callback // for stats etc. console.log('transferred %s', path); }, function(err){ // this second optional callback is used when // an error occurs during transmission }); res.download(path);});
// error handling middleware. Because it's// below our routes, you will be able to// "intercept" errors, otherwise Connect// will respond with 500 "Internal Server Error".app.use(function(err, req, res, next){ if ('ENOENT' == err.code) { // log all errors console.error(err.stack);
// special-case 404s if (404 == err.status) { res.send('Cant find that file, sorry!'); } else { // Not a 404 next(err); }});
2 unmodified linesmodified:lib/response.js+3 added-1 removed
208 unmodified lines209 // cast ENOENT210 if ('ENOENT' == err.code) err = 404;211 212 // coerce numeric error to an Error213+ // TODO: remove214+ // TODO: remove docs for headerSent?215 if ('number' == typeof err) err = utils.error(err);216 217 // ditch content-disposition to prevent funky responses218 if (!self.headerSent) self.removeHeader('Content-Disposition');20 unmodified lines239 *240 * Optionally providing an alternate attachment `filename`,241 * and optional callback `fn(err)`. The callback is invoked242 * when the data transfer is complete, or when an error has241- * ocurred. Be sure to check `res.sendHeader` if you plan to respond.243+ * ocurred. Be sure to check `res.headerSent` if you plan to respond.244 *245 * @param {String} path246 * @param {String|Function} filename or fn247 * @param {Function} fn351 unmodified lines