Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom mime-type(s) via .types file #152

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: node_js
node_js:
- 0.8
- 0.10
- 0.12
- iojs
before_install:
- curl --location http://git.io/1OcIZA | bash -s
branches:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ This will install `http-server` globally so that it may be run from the command

`-r` or `--robots` Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')

`-m` or `--mimetype` Define custom mime-type(s) by path to [.types](http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types) file

`-h` or `--help` Print this list and exit.
28 changes: 20 additions & 8 deletions bin/http-server
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env node

'use strict';

var colors = require('colors'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
opener = require('opener'),
argv = require('optimist')
opener = require('opener'),
path = require('path'),
argv = require('optimist')
.boolean('cors')
.argv;

Expand All @@ -31,13 +34,14 @@ if (argv.h || argv.help) {
" -K --key Path to ssl key file (default: key.pem).",
"",
" -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]",
" -m --mimetype Define custom mime-type(s) [Content-Type: custom-mime-type]",
" -h --help Print this list and exit."
].join('\n'));
process.exit();
}

var port = argv.p || parseInt(process.env.PORT, 10),
host = argv.a || '0.0.0.0',
host = argv.a || process.env.IP || '0.0.0.0', // TODO: remove process.env.IP - just for debugging on c9
log = (argv.s || argv.silent) ? (function () {}) : console.log,
ssl = !!argv.S || !!argv.ssl,
proxy = argv.P || argv.proxy,
Expand Down Expand Up @@ -73,11 +77,13 @@ function listen(port) {
robots: argv.r || argv.robots,
ext: argv.e || argv.ext,
logFn: requestLogger,
proxy: proxy
proxy: proxy,
mimeType: argv.m || argv.mimetype,
cors: !!argv.cors
};

if (argv.cors) {
options.cors = true;
if(options.mimeType && typeof options.mimeType === 'string') {
options.mimeType = path.resolve(options.mimeType);
}

if (ssl) {
Expand All @@ -87,7 +93,13 @@ function listen(port) {
};
}

var server = httpServer.createServer(options);
var server;
try {
server = httpServer.createServer(options);
} catch (e) {
log(e.message.red);
process.exit(1);
}
server.listen(port, host, function () {
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
protocol = ssl ? 'https:' : 'http:';
Expand Down
5 changes: 4 additions & 1 deletion lib/http-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var fs = require('fs'),
util = require('util'),
union = require('union'),
ecstatic = require('ecstatic'),
httpProxy = require('http-proxy'),
Expand All @@ -26,6 +27,7 @@ var HTTPServer = exports.HTTPServer = function (options) {
this.cache = options.cache || 3600; // in seconds.
this.showDir = options.showDir !== 'false';
this.autoIndex = options.autoIndex !== 'false';
this.mimeType = options.mimeType;

if (options.ext) {
this.ext = options.ext === true
Expand Down Expand Up @@ -70,6 +72,7 @@ var HTTPServer = exports.HTTPServer = function (options) {
cache: this.cache,
showDir: this.showDir,
autoIndex: this.autoIndex,
mimeType: this.mimeType,
defaultExt: this.ext,
handleError: typeof options.proxy !== 'string'
}));
Expand Down
26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-server",
"version": "0.8.0",
"version": "0.9.0",
"description": "A simple zero-configuration command-line http server",
"main": "./lib/http-server",
"repository": {
Expand Down Expand Up @@ -54,23 +54,27 @@
},
{
"name": "Jinkwon Lee",
"email" : "master@bdyne.net"
"email": "master@bdyne.net"
},
{
"name": "Jon Ege Ronnenberg",
"email": "jon.ronnenberg@gmail.com"
}
],
"dependencies": {
"colors": "1.0.3",
"optimist": "0.6.x",
"union": "~0.4.3",
"ecstatic": "~0.7.0",
"corser": "~2.0.0",
"ecstatic":"dotnetCarpenter/node-ecstatic",
"http-proxy": "^1.8.1",
"portfinder": "0.4.x",
"opener": "~1.4.0",
"corser": "~2.0.0"
"optimist": "0.6.x",
"portfinder": "0.4.x",
"union": "~0.4.3"
},
"devDependencies": {
"vows": "0.7.x",
"request": "2.49.x",
"stylezero": "2.2.0"
"stylezero": "2.2.0",
"vows": "0.7.x"
},
"bugs": {
"url": "https://github.com/nodeapps/http-server/issues"
Expand All @@ -81,9 +85,9 @@
"url": "https://github.com/nodeapps/http-server/raw/master/LICENSE"
}
],
"preferGlobal": "true",
"preferGlobal": true,
"bin": {
"http-server": "./bin/http-server",
"hs" : "./bin/http-server"
"hs": "./bin/http-server"
}
}
Empty file added test/fixtures/root/mime.test
Empty file.
11 changes: 11 additions & 0 deletions test/fixtures/root/mime.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is an example of the Apache .types file format for describing mime-types.
# Other example: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
text/prs.test test
video/mp4 mp4 m4v
video/ogg ogv
video/webm webm
text/cache-manifest manifest
application/x-web-app-manifest+json webapp
application/vnd.apple.mpegURL M3U3
video/MP2T ts
text/css less
47 changes: 47 additions & 0 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var assert = require('assert'),
path = require('path'),
fs = require('fs'),
Expand Down Expand Up @@ -150,5 +152,50 @@ vows.describe('http-server').addBatch({
assert.equal(res.statusCode, 204);
}
}
},
'When using custom mime-types from a .types file': {
topic: function () {
var server = httpServer.createServer({
root: root,
mimeType: 'fixtures/root/mime.types'
});

server.listen(8083, '127.0.0.1', this.callback);
},
'and when asked for a .test file': {
topic: function () {
// give http-server an extra second before requesting
// to not get ECONNREFUSED
var cb = this.callback.bind(this);
setTimeout(function () {
request('http://127.0.0.1:8083/mime.test', cb);
}, 2000);
},
'it should answer with text/prs.test': function (err, res, body) {
assert.isNull(err);
assert.equal(res.headers['content-type'], 'text/prs.test; charset=UTF-8');
}
}
},
'When custom mime-types are added as an option': {
topic: function () {
var server = httpServer.createServer({
root: root,
mimeType: {
'text/prs.test2': ['test']
}
});
server.listen(8084);
return server;
},
'and when asked for a .test file': {
topic: function () {
request('http://127.0.0.1:8084/mime.test', this.callback);
},
'it should answer with text/prs.test2': function (err, res, body) {
assert.isNull(err);
assert.equal(res.headers['content-type'], 'text/prs.test2; charset=UTF-8');
}
}
}
}).export(module);