Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ This will install `http-server` globally so that it may be run from the command

`-U` or `--utc` Use UTC time format in log messages.

`-H` or `--header` Add custom headers. Format is `-H=header:value`. Specify `-H` multiple times to set multiple headers.

`-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com

`-S` or `--ssl` Enable https.
Expand Down
19 changes: 19 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var colors = require('colors/safe'),
opener = require('opener'),
argv = require('optimist')
.boolean('cors')
.alias('header', 'H')
.argv;

var ifaces = os.networkInterfaces();
Expand All @@ -31,6 +32,7 @@ if (argv.h || argv.help) {
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
' To disable caching, use -c-1.',
' -U --utc Use UTC time format in log messages.',
' -H --header=header[:value] Add custom headers. Specify -H for each header to be set.',
'',
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
'',
Expand Down Expand Up @@ -113,6 +115,23 @@ function listen(port) {
}
}

function setHeader(h) {
var header = h.split(':');
options.headers[header[0]] = (header[1] ? header[1] : '');
}

if (argv.header) {
options.headers = {};
if (Array.isArray(argv.header)) {
argv.header.forEach(function (h) {
setHeader(h);
});
}
else {
setHeader(argv.header);
}
}

if (ssl) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
Expand Down
11 changes: 10 additions & 1 deletion test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ vows.describe('http-server').addBatch({
robots: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true'
'Access-Control-Allow-Credentials': 'true',
'my-header': 'headerValue'
}
});

Expand Down Expand Up @@ -76,6 +77,14 @@ vows.describe('http-server').addBatch({
assert.equal(res.headers['access-control-allow-credentials'], 'true');
}
},
'and options include static http-headers': {
topic: function () {
request('http://127.0.0.1:8080/', this.callback);
},
'should respond with headers set in options': function (err, res) {
assert.equal(res.headers['my-header'], 'headerValue');
}
},
'When http-server is proxying from 8081 to 8080': {
topic: function () {
var proxyServer = httpServer.createServer({
Expand Down