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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ This will install `http-server` globally so that it may be run from the command
`-h` or `--help` Displays a list of commands and exits.

`-c` Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds. To disable caching, use -c-1.

`-H` or `--header` Send the specified header with responses, e.g.
`-H "Strict-Transport-Security: max-age=315360000; includeSubdomains"`.
15 changes: 14 additions & 1 deletion bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if (argv.h || argv.help) {
" -o Open browser window after staring the server",
" -c Set cache time (in seconds). e.g. -c10 for 10 seconds.",
" To disable caching, use -c-1.",
" -H --header Send the specified header with responses,",
" e.g. -H 'X-MY-HEADER: foo'",
" -h --help Print this list and exit."
].join('\n'));
process.exit();
Expand All @@ -47,20 +49,31 @@ if (!port) {
listen(port);
}

function addHeader(headers, header) {
if (!header || !header.indexOf) return;
var idx = header.indexOf(':'),
key = header.substring(0, idx),
val = header.substring(idx + 1).trim();
headers[key] = val;
}

function listen(port) {
var options = {
root: argv._[0],
cache: argv.c,
showDir: argv.d,
autoIndex: argv.i,
ext: argv.e || argv.ext,
headers: {},
logFn: requestLogger
};

if (argv.cors) {
options.headers = { 'Access-Control-Allow-Origin': '*' };
options.headers['Access-Control-Allow-Origin'] = '*';
}

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

var server = httpServer.createServer(options);
server.listen(port, host, function() {
log('Starting up http-server, serving '.yellow
Expand Down