Skip to content

Commit

Permalink
Merge branch 'download-example'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 18, 2010
2 parents a33fb0c + bbe65a8 commit f6ba793
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/downloads/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

/**
* Module dependencies.
*/

var express = require('./../../lib/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>'
+ '</ul>');
});

app.get('/files/*', function(req, res){
var file = req.params[0];
res.download(__dirname + '/files/' + file);
});

app.error(function(err, req, res, next){
if (process.ENOENT == err.errno) {
res.send('Cant find that file, sorry!');
} else {
// Not a 404
next(err);
}
});

app.listen(3000);
console.log('Express started on port 3000');
1 change: 1 addition & 0 deletions examples/downloads/files/amazing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wow supes cool are you glad you downloaded this?
1 change: 1 addition & 0 deletions lib/express/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ http.ServerResponse.prototype.sendfile = function(path, fn){
var self = this;
fs.readFile(path, function(err, buf){
if (err) {
delete self.headers['Content-Disposition'];
if (fn) {
fn(err, path);
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ module.exports = {
res.download(__dirname + '/fixtures/' + req.params[0]);
});

app.error(function(err, req, res){
res.send(404);
})

assert.response(app,
{ url: '/user.json' },
{ body: '{"name":"tj"}', status: 200, headers: {
Expand All @@ -237,6 +241,12 @@ module.exports = {
'Content-Type': 'application/json',
'Content-Disposition': 'attachment; filename="account.json"'
}});

assert.response(app,
{ url: '/missing' },
function(res){
assert.equal(null, res.headers['content-disposition']);
});
},

'test #cookie()': function(assert){
Expand Down

0 comments on commit f6ba793

Please sign in to comment.