Skip to content

Commit 415e1c3

Browse files
jesseditsonJesse Ditson
authored andcommitted
Add baseDir option, add -b flag to cli options to specify base path
1 parent 9432968 commit 415e1c3

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ This will install `http-server` globally so that it may be run from the command
3333

3434
`-a` Address to use (defaults to 0.0.0.0)
3535

36+
`-b` Base path to serve files from (defaults /)
37+
3638
`-d` Show directory listings (defaults to `true`)
3739

3840
`-i` Display autoIndex (defaults to `true`)

bin/http-server

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if (argv.h || argv.help) {
2121
' -p Port to use [8080]',
2222
' -a Address to use [0.0.0.0]',
2323
' -d Show directory listings [true]',
24+
' -b Base directory to serve files from [/]',
2425
' -i Display autoIndex [true]',
2526
' -g --gzip Serve gzip files when possible [false]',
2627
' -e --ext Default file extension if none supplied [none]',
@@ -50,6 +51,7 @@ var port = argv.p || parseInt(process.env.PORT, 10),
5051
ssl = !!argv.S || !!argv.ssl,
5152
proxy = argv.P || argv.proxy,
5253
utc = argv.U || argv.utc,
54+
baseDir = argv.b,
5355
logger;
5456

5557
if (!argv.s && !argv.silent) {
@@ -97,6 +99,7 @@ function listen(port) {
9799
root: argv._[0],
98100
cache: argv.c,
99101
showDir: argv.d,
102+
baseDir: baseDir,
100103
autoIndex: argv.i,
101104
gzip: argv.g || argv.gzip,
102105
robots: argv.r || argv.robots,
@@ -123,7 +126,8 @@ function listen(port) {
123126
var server = httpServer.createServer(options);
124127
server.listen(port, host, function () {
125128
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
126-
protocol = ssl ? 'https://' : 'http://';
129+
protocol = ssl ? 'https://' : 'http://',
130+
path = baseDir ? '/' + baseDir.replace(/^\//, '') : '';
127131

128132
logger.info([colors.yellow('Starting up http-server, serving '),
129133
colors.cyan(server.root),
@@ -151,7 +155,7 @@ function listen(port) {
151155
logger.info('Hit CTRL-C to stop the server');
152156
if (argv.o) {
153157
opener(
154-
protocol + canonicalHost + ':' + port,
158+
protocol + canonicalHost + ':' + port + path,
155159
{ command: argv.o !== true ? argv.o : null }
156160
);
157161
}

lib/http-server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function HttpServer(options) {
9595

9696
before.push(ecstatic({
9797
root: this.root,
98+
baseDir: options.baseDir,
9899
cache: this.cache,
99100
showDir: this.showDir,
100101
showDotfiles: this.showDotfiles,

test/http-server-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,45 @@ vows.describe('http-server').addBatch({
154154
assert.ok(res.headers['access-control-allow-headers'].split(/\s*,\s*/g).indexOf('X-Test') >= 0, 204);
155155
}
156156
}
157+
},
158+
'When baseDir is specified': {
159+
topic: function () {
160+
var server = httpServer.createServer({
161+
root: root,
162+
baseDir: '/test'
163+
});
164+
server.listen(8083);
165+
this.callback(null, server);
166+
},
167+
'it should serve files at the specified baseDir': {
168+
topic: function () {
169+
request('http://127.0.0.1:8083/test/file', this.callback);
170+
},
171+
'status code should be 200': function (res) {
172+
assert.equal(res.statusCode, 200);
173+
},
174+
'and file content': {
175+
topic: function (res, body) {
176+
var self = this;
177+
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
178+
self.callback(err, data, body);
179+
});
180+
},
181+
'should match content of served file': function (err, file, body) {
182+
assert.equal(body.trim(), file.trim());
183+
}
184+
}
185+
},
186+
'it should not serve files at the root': {
187+
topic: function () {
188+
request('http://127.0.0.1:8083/file', this.callback);
189+
},
190+
'status code should be 403': function (res) {
191+
assert.equal(res.statusCode, 403);
192+
},
193+
'and file content should be empty': function (res) {
194+
assert.equal(res.body, '');
195+
}
196+
}
157197
}
158198
}).export(module);

0 commit comments

Comments
 (0)