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 @@ -83,6 +83,8 @@ Using `npx` you can run the script without installing it first:
`-r` or `--robots` Provide a /robots.txt (whose content defaults to `User-agent: *\nDisallow: /`)

`--no-dotfiles` Do not show dotfiles

`-H` or `--header` Send the specified header with responses, e.g. `-H "Strict-Transport-Security: max-age=315360000; includeSubdomains"`.

`-h` or `--help` Print this list and exit.

Expand Down
19 changes: 19 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ if (argv.h || argv.help) {
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
' -H --header Send the specified header with responses,',
' e.g. -H \'X-MY-HEADER: foo\'',
' -h --help Print this list and exit.',
' -v --version Print the version and exit.'
].join('\n'));
Expand Down Expand Up @@ -113,6 +115,20 @@ else {
listen(port);
}

function addHeader(headers, headersList) {
if (!Array.isArray(headersList)) {
headersList = [headersList];
}

for (var i in headersList) {
var idx = headersList[i].indexOf(':'),
key = headersList[i].substring(0, idx),
val = headersList[i].substring(idx + 1).trim();

headers[key] = val;
}
}

function listen(port) {
var options = {
root: argv._[0],
Expand All @@ -127,6 +143,7 @@ function listen(port) {
logFn: logger.request,
proxy: proxy,
showDotfiles: argv.dotfiles,
headers: {},
username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME,
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD
};
Expand Down Expand Up @@ -159,6 +176,8 @@ function listen(port) {
}
}

addHeader(options.headers, argv.H || argv.header);

var server = httpServer.createServer(options);
server.listen(port, host, function () {
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
Expand Down
26 changes: 26 additions & 0 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,5 +514,31 @@ vows.describe('http-server').addBatch({
teardown: function (server) {
server.close();
}
},
'When custom headers are set': {
topic: function () {
var server = httpServer.createServer({
root: root,
headers: {
'X-Custom': 'Test'
}
});
server.listen(8087);
this.callback(null, server);
},
requesting: {
topic: function () {
request('http://127.0.0.1:8087/', this.callback);
},
'should respond with status code 200': function (err, res) {
assert.equal(res.statusCode, 200);
},
'and response X-Custom header should contain 1': function (err, res) {
assert.ok(res.headers['x-custom']);
}
},
teardown: function (server) {
server.close();
}
}
}).export(module);