Skip to content

Commit 7be905c

Browse files
feat: add flag --content-type
Adds a flag to change the default Content-Type header value to something other than application/octet-stream. This feature may not have the demand that it would have before the internalization of ecstatic, however contentType was already managed as an option and there just wasn't a flag in front of it, so I figured I'd finish what was started. Co-authored-by: howardroark <[email protected]>
1 parent f6306d5 commit 7be905c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

bin/http-server

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ if (argv.h || argv.help) {
4242
'',
4343
' -e --ext Default file extension if none supplied [none]',
4444
' -s --silent Suppress log messages from output',
45+
' --content-type Default content type for unknown file types [application/octet-stream]',
4546
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
4647
' When enabled, sets Access-Control-Allow-Origin to "*"',
4748
' Optional value adds to Access-Control-Allow-Headers',
@@ -169,6 +170,7 @@ function listen(port) {
169170
proxyOptions: proxyOptions,
170171
showDotfiles: argv.dotfiles,
171172
mimetypes: argv.mimetypes,
173+
contentType: argv['content-type'],
172174
username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME,
173175
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD,
174176
headers: {}

lib/http-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function HttpServer(options) {
7575
: options.ext;
7676
}
7777
this.contentType = options.contentType ||
78-
this.ext === 'html' ? 'text/html' : 'application/octet-stream';
78+
(this.ext === 'html' ? 'text/html' : 'application/octet-stream');
7979

8080
var before = options.before ? options.before.slice() : [];
8181

test/cli.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,22 @@ test('empty header value is allowed (RFC 7230)', (t) => {
177177
{ 'x-http-server-test-empty-a': '', 'x-http-server-test-empty-b': '' }
178178
);
179179
});
180+
181+
test('setting default content-type via cli', (t) => {
182+
t.plan(4);
183+
184+
getPort().then((port) => {
185+
const root = path.resolve(__dirname, 'public/');
186+
const options = [root, '--port', port, '--content-type', 'text/custom'];
187+
const server = startServer(options);
188+
189+
tearDown(server, t);
190+
191+
server.stdout.on('data', (msg) => {
192+
checkServerIsRunning(`http://localhost:${port}/f_f`, msg, t, (err, res) => {
193+
t.error(err);
194+
t.equal(res.headers['content-type'], 'text/custom; charset=UTF-8');
195+
});
196+
});
197+
});
198+
});

0 commit comments

Comments
 (0)