Skip to content

Implement custom header to responses. #822

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This will install `http-server` globally so that it may be run from the command
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |
|`-K` or `--key` |Path to ssl key file |`key.pem` |
|`-H` or `--header` |Add extra header to all response | |
|`-r` or `--robots` | Automatically provide a /robots.txt (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` |
|`--no-dotfiles` |Do not show dotfiles| |
|`--mimetypes` |Path to a .types file for custom mimetype definition| |
Expand Down
11 changes: 11 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if (argv.h || argv.help) {
' -S --tls --ssl Enable secure request serving with TLS/SSL (HTTPS)',
' -C --cert Path to TLS cert file (default: cert.pem)',
' -K --key Path to TLS key file (default: key.pem)',
' -H --header Add extra header to all response, eg. "X-Frame-Options: DENY"',
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
Expand Down Expand Up @@ -173,6 +174,16 @@ function listen(port) {
}
}

var extraHeaders = argv.H || argv.header;
if (extraHeaders) {
if (Array.isArray(extraHeaders)) {
options.extraHeaders = extraHeaders;
}
else {
options.extraHeaders = [extraHeaders];
}
}

if (tls) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
Expand Down
4 changes: 4 additions & 0 deletions doc/http-server.1
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Path to SSL key file.
If not specified, uses key.pem.
Passphrase will be read from NODE_HTTP_SERVER_SSL_PASSPHRASE (if set)

.TP
.BI \-H ", " \-\-header " " [\fIHEADER\fR]
Add extra header to all response.

.TP
.BI \-r ", " \-\-robots " " [\fIUSER\-AGENT\fR]
Respond to /robots.txt request.
Expand Down
7 changes: 7 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ function HttpServer(options) {
} : null));
}

if (options.extraHeaders) {
options.extraHeaders.forEach(function (header) {
var split = header.split(/:(.+)?/);
this.headers[split[0]] = split[1];
}, this);
}

if (options.robots) {
before.push(function (req, res) {
if (req.url === '/robots.txt') {
Expand Down
10 changes: 9 additions & 1 deletion test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ test('http-server main', (t) => {
corsHeaders: 'X-Test',
ext: true,
brotli: true,
gzip: true
gzip: true,
extraHeaders: [
'Authorization:CustomToken',
'X-API-Key:VerySecureAndRandomToken'
]
});
server.listen(8080, async () => {
try {
Expand Down Expand Up @@ -63,6 +67,10 @@ test('http-server main', (t) => {
// Custom headers
t.equal(res.headers['access-control-allow-origin'], '*');
t.equal(res.headers['access-control-allow-credentials'], 'true');

// Custom extra headers
t.equal(res.headers['authorization'], 'CustomToken');
t.equal(res.headers['x-api-key'], 'VerySecureAndRandomToken');
}).catch(err => t.fail(err.toString())),

// Get robots
Expand Down