Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from jprichardson/master

Implemented an option for the default Content-Type
  • Loading branch information...
commit 27edaf4da6525f7f7470f5ea25898e91a1c1bc1f 2 parents fc3b63c + 6764601
@carlos8f authored
View
1  README.md
@@ -91,6 +91,7 @@ Options
- `notFoundPath`: Path to be rendered on `buffetMiddleware.notFound`. (Default:
`/404.html`)
- `keepAlive`: Timeout (in milliseconds) for HTTP keep-alive. (Default: `5000`)
+- `defaultContentType`: If the file does not have an extension, set this to specify the default `Content-Type` sent to the browser. This defaults to `application/octet-stream`.
Running your own benchmark
--------------------------
View
4 lib/file.js
@@ -8,6 +8,7 @@ var fs = require('fs')
, pkgInfo = require('../package.json')
, poweredBy = pkgInfo.name + ' ' + pkgInfo.version
, gzippable = require('./gzippable')
+ , path = require('path')
;
function File(file, options) {
@@ -17,6 +18,9 @@ function File(file, options) {
this.keepAlive = this.options.keepAlive && Math.round(this.options.keepAlive / 1000);
this.file = file;
this.mime = mime.lookup(this.file);
+ if (path.extname(file) === '') {
+ this.mime = options.defaultContentType;
+ }
this.cache();
}
inherits(File, EventEmitter);
View
1  lib/index.js
@@ -19,6 +19,7 @@ module.exports = function buffet(root, opts) {
options[opt] = true;
}
});
+ options.defaultContentType = options.defaultContentType || 'application/octet-stream';
if (typeof options.keepAlive === 'undefined') {
options.keepAlive = 5000;
}
View
2  package.json
@@ -2,7 +2,7 @@
"author": "Carlos Rodriguez <carlos@s8f.org> (http://s8f.org/)",
"name": "buffet",
"description": "Performance-oriented static file server",
- "version": "0.4.5",
+ "version": "0.4.6",
"main": "./lib",
"scripts": {
"test": "make test"
View
14 test/files/index
@@ -0,0 +1,14 @@
+<html>
+ <head>
+ <title>node-buffet test</title>
+ <style>
+ body { background-image: url(folder/Alice-white-rabbit.jpg); }
+ </style>
+ </head>
+ <body>
+ <h1>node-buffet</h1>
+ <ul>
+ <li><a href="hello.txt">hello.html</a></li>
+ </ul>
+ </body>
+</html>
View
63 test/simple.js
@@ -120,6 +120,69 @@ describe('simple test', function() {
req.end();
});
+ describe('defaultContentType', function() {
+ var dcPort = 42917
+ , dcBaseUrl = 'http://localhost:' + dcPort
+ , dcTestFolder = '/tmp/buffet-test-' + idgen()
+ , defaultType = 'text/crazy'
+ ;
+
+ beforeEach(function(done) {
+ ncp('test/files', dcTestFolder, done);
+ });
+
+ afterEach(function(done) {
+ if (cleanup) {
+ rimraf(dcTestFolder, done);
+ } else {
+ done();
+ }
+ });
+
+ it('serves the proper content type even if the mime is detected', function(done) {
+ //i think there is a bug in node module fs-watch-tree. set watch to true and you'll
+ //see the test 'serves an updated file' fail occasionally
+ var handler = buffet(dcTestFolder, {defaultContentType: defaultType, watch: false});
+
+ var server = http.createServer(handler).listen(dcPort, function() {
+ var req = http.get(dcBaseUrl + '/index.html', function(res) {
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'text/html');
+ req.end();
+ server.close(done);
+ });
+ });
+ });
+
+ it('serves the default content type specified in the options if it cant detect the mime', function(done) {
+ var handler = buffet(testFolder, {defaultContentType: defaultType, watch: false});
+
+ var server = http.createServer(handler).listen(dcPort, function() {
+ var req = http.get(dcBaseUrl + '/index', function(res) {
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], defaultType);
+ req.end();
+ server.close(done);
+ });
+ });
+ });
+
+ it('serves the application/octet-stream when no defaultContentType is and it cant detect the mime', function(done) {
+ var handler = buffet(testFolder, {watch: false});
+
+ var server = http.createServer(handler).listen(dcPort, function() {
+ var req = http.get(dcBaseUrl + '/index', function(res) {
+ assert.equal(res.statusCode, 200);
+ assert.equal(res.headers['content-type'], 'application/octet-stream');
+ req.end();
+ server.close(done);
+ });
+ });
+ });
+
+
+ });
+
describe('watcher', function() {
var testData = {yay: true}, folderName = idgen();
before(function(done) {
Please sign in to comment.
Something went wrong with that request. Please try again.