Skip to content

Commit

Permalink
bogart.file adds appropriate content type based on mime type of file …
Browse files Browse the repository at this point in the history
…extension
  • Loading branch information
nrstott committed Sep 23, 2011
1 parent e2f54fe commit 9d2ebf3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/pipe-file.js
Expand Up @@ -17,4 +17,10 @@ app.get('/image.jpg', function(req) {
});
});

app.get('/cat.jpg', function(req) {
var filePath = path.join(__dirname, 'static-server', 'public', 'images', 'ninja-cat.jpg');

return bogart.file(filePath);
});

bogart.start(app);
14 changes: 14 additions & 0 deletions lib/bogart.js
Expand Up @@ -580,6 +580,16 @@ exports.pipe = function(stream, opts) {
}
};

/**
* Get MIME type for a file extension
*
* @param {String} ext File extension
* @returns {String} MIME type of file extension.
*/
exports.mimeType = function(ext) {
return require("./mimetypes").mimeType(ext);
};

/**
* Creates a JSGI response that streams a file
*
Expand All @@ -589,6 +599,10 @@ exports.pipe = function(stream, opts) {
* @returns {Promise} A promise for a JSGI response
*/
exports.file = function(filePath, opts) {
opts = opts || {};
opts.headers = opts.headers || {};
opts.headers['Content-Type'] = opts.headers['Content-Type'] || exports.mimeType(path.extname(filePath));

return exports.pipe(fs.createReadStream(filePath), opts);
};

Expand Down

0 comments on commit 9d2ebf3

Please sign in to comment.