Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Implemented an option for the default Content-Type #6

Merged
merged 6 commits into from over 1 year ago

2 participants

JP Richardson Carlos Rodriguez
JP Richardson

Implemented an option for the default content-type. Check it out.

Carlos Rodriguez
Owner

looks good, thank you! I may do some cleanups later (mime.lookup() takes a second param for a fallback content type).

Carlos Rodriguez carlos8f merged commit 27edaf4 into from
Carlos Rodriguez carlos8f closed this
JP Richardson

Awesome! Will you publish the changes to the NPM? ;)

Carlos Rodriguez
Owner

i want to clean up the tests a bit, but then i'll publish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  README.md
Source Rendered
@@ -91,6 +91,7 @@ Options
91 91
 - `notFoundPath`: Path to be rendered on `buffetMiddleware.notFound`. (Default:
92 92
   `/404.html`)
93 93
 - `keepAlive`: Timeout (in milliseconds) for HTTP keep-alive. (Default: `5000`)
  94
+- `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`.
94 95
 
95 96
 Running your own benchmark
96 97
 --------------------------
4  lib/file.js
@@ -8,6 +8,7 @@ var fs = require('fs')
8 8
   , pkgInfo = require('../package.json')
9 9
   , poweredBy = pkgInfo.name + ' ' + pkgInfo.version
10 10
   , gzippable = require('./gzippable')
  11
+  , path = require('path')
11 12
   ;
12 13
 
13 14
 function File(file, options) {
@@ -17,6 +18,9 @@ function File(file, options) {
17 18
   this.keepAlive = this.options.keepAlive && Math.round(this.options.keepAlive / 1000);
18 19
   this.file = file;
19 20
   this.mime = mime.lookup(this.file);
  21
+  if (path.extname(file) === '') {
  22
+    this.mime = options.defaultContentType;
  23
+  }
20 24
   this.cache();
21 25
 }
22 26
 inherits(File, EventEmitter);
1  lib/index.js
@@ -19,6 +19,7 @@ module.exports = function buffet(root, opts) {
19 19
       options[opt] = true;
20 20
     }
21 21
   });
  22
+  options.defaultContentType = options.defaultContentType || 'application/octet-stream';
22 23
   if (typeof options.keepAlive === 'undefined') {
23 24
     options.keepAlive = 5000;
24 25
   }
2  package.json
@@ -2,7 +2,7 @@
2 2
   "author": "Carlos Rodriguez <carlos@s8f.org> (http://s8f.org/)",
3 3
   "name": "buffet",
4 4
   "description": "Performance-oriented static file server",
5  
-  "version": "0.4.5",
  5
+  "version": "0.4.6",
6 6
   "main": "./lib",
7 7
   "scripts": {
8 8
     "test": "make test"
14  test/files/index
... ...
@@ -0,0 +1,14 @@
  1
+<html>
  2
+  <head>
  3
+    <title>node-buffet test</title>
  4
+    <style>
  5
+      body { background-image: url(folder/Alice-white-rabbit.jpg); }
  6
+    </style>
  7
+  </head>
  8
+  <body>
  9
+    <h1>node-buffet</h1>
  10
+    <ul>
  11
+      <li><a href="hello.txt">hello.html</a></li>
  12
+    </ul>
  13
+  </body>
  14
+</html>
63  test/simple.js
@@ -120,6 +120,69 @@ describe('simple test', function() {
120 120
     req.end();
121 121
   });
122 122
 
  123
+  describe('defaultContentType', function() {
  124
+    var dcPort = 42917
  125
+      , dcBaseUrl = 'http://localhost:' + dcPort
  126
+      , dcTestFolder = '/tmp/buffet-test-' + idgen()
  127
+      , defaultType = 'text/crazy'
  128
+      ;
  129
+
  130
+    beforeEach(function(done) {
  131
+      ncp('test/files', dcTestFolder, done);
  132
+    });
  133
+
  134
+    afterEach(function(done) {
  135
+      if (cleanup) {
  136
+        rimraf(dcTestFolder, done);
  137
+      } else {
  138
+        done();
  139
+      }
  140
+    });
  141
+
  142
+    it('serves the proper content type even if the mime is detected', function(done) {
  143
+      //i think there is a bug in node module fs-watch-tree. set watch to true and you'll
  144
+      //see the test 'serves an updated file' fail occasionally
  145
+      var handler = buffet(dcTestFolder, {defaultContentType: defaultType, watch: false}); 
  146
+         
  147
+      var server = http.createServer(handler).listen(dcPort, function() {
  148
+        var req = http.get(dcBaseUrl + '/index.html', function(res) {
  149
+          assert.equal(res.statusCode, 200);
  150
+          assert.equal(res.headers['content-type'], 'text/html');
  151
+          req.end();
  152
+          server.close(done);
  153
+        });
  154
+      });
  155
+    });
  156
+
  157
+    it('serves the default content type specified in the options if it cant detect the mime', function(done) {
  158
+      var handler = buffet(testFolder, {defaultContentType: defaultType, watch: false}); 
  159
+
  160
+      var server = http.createServer(handler).listen(dcPort, function() {
  161
+        var req = http.get(dcBaseUrl + '/index', function(res) {
  162
+          assert.equal(res.statusCode, 200);
  163
+          assert.equal(res.headers['content-type'], defaultType);
  164
+          req.end();
  165
+          server.close(done);
  166
+        });
  167
+      });
  168
+    });
  169
+
  170
+    it('serves the application/octet-stream when no defaultContentType is and it cant detect the mime', function(done) {
  171
+      var handler = buffet(testFolder, {watch: false}); 
  172
+
  173
+      var server = http.createServer(handler).listen(dcPort, function() {
  174
+        var req = http.get(dcBaseUrl + '/index', function(res) {
  175
+          assert.equal(res.statusCode, 200);
  176
+          assert.equal(res.headers['content-type'], 'application/octet-stream');
  177
+          req.end();
  178
+          server.close(done);
  179
+        });
  180
+      });
  181
+    });
  182
+
  183
+  
  184
+  });
  185
+
123 186
   describe('watcher', function() {
124 187
     var testData = {yay: true}, folderName = idgen();
125 188
     before(function(done) {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.