Skip to content

Commit 6201b9b

Browse files
author
Ivan
committed
Merge branch 'base_path' of https://github.com/jesseditson/http-server into add-base-dir
2 parents 29e6c6a + ed1d639 commit 6201b9b

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ if (argv.h || argv.help) {
2222
' -p Port to use [8080]',
2323
' -a Address to use [0.0.0.0]',
2424
' -d Show directory listings [true]',
25+
' -b Base directory to serve files from [/]',
2526
' -i Display autoIndex [true]',
2627
' -g --gzip Serve gzip files when possible [false]',
2728
' -e --ext Default file extension if none supplied [none]',
@@ -51,6 +52,7 @@ var port = argv.p || parseInt(process.env.PORT, 10),
5152
ssl = !!argv.S || !!argv.ssl,
5253
proxy = argv.P || argv.proxy,
5354
utc = argv.U || argv.utc,
55+
baseDir = argv.b,
5456
logger;
5557

5658
if (!argv.s && !argv.silent) {
@@ -99,6 +101,7 @@ function listen(port) {
99101
root: argv._[0],
100102
cache: argv.c,
101103
showDir: argv.d,
104+
baseDir: baseDir,
102105
autoIndex: argv.i,
103106
gzip: argv.g || argv.gzip,
104107
robots: argv.r || argv.robots,
@@ -125,7 +128,8 @@ function listen(port) {
125128
var server = httpServer.createServer(options);
126129
server.listen(port, host, function () {
127130
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
128-
protocol = ssl ? 'https://' : 'http://';
131+
protocol = ssl ? 'https://' : 'http://',
132+
path = baseDir ? '/' + baseDir.replace(/^\//, '') : '';
129133

130134
logger.info([colors.yellow('Starting up http-server, serving '),
131135
colors.cyan(server.root),
@@ -134,13 +138,13 @@ function listen(port) {
134138
].join(''));
135139

136140
if (argv.a && host !== '0.0.0.0') {
137-
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString())));
141+
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString()) + path));
138142
}
139143
else {
140144
Object.keys(ifaces).forEach(function (dev) {
141145
ifaces[dev].forEach(function (details) {
142146
if (details.family === 'IPv4') {
143-
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
147+
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString()) + path));
144148
}
145149
});
146150
});
@@ -153,7 +157,7 @@ function listen(port) {
153157
logger.info('Hit CTRL-C to stop the server');
154158
if (argv.o) {
155159
opener(
156-
protocol + canonicalHost + ':' + port,
160+
protocol + canonicalHost + ':' + port + path,
157161
{ command: argv.o !== true ? argv.o : null }
158162
);
159163
}

lib/http-server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function HttpServer(options) {
9797
before.push(ecstatic({
9898
baseDir: this.baseDir,
9999
root: this.root,
100+
baseDir: options.baseDir,
100101
cache: this.cache,
101102
showDir: this.showDir,
102103
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)