Skip to content

Commit

Permalink
Merge branch 'base_path' of https://github.com/jesseditson/http-server
Browse files Browse the repository at this point in the history
…into add-base-dir
  • Loading branch information
Ivan committed Jun 23, 2018
2 parents 29e6c6a + ed1d639 commit 6201b9b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ This will install `http-server` globally so that it may be run from the command

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

`-b` Base path to serve files from (defaults /)

`-d` Show directory listings (defaults to `true`)

`-i` Display autoIndex (defaults to `true`)
Expand Down
12 changes: 8 additions & 4 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if (argv.h || argv.help) {
' -p Port to use [8080]',
' -a Address to use [0.0.0.0]',
' -d Show directory listings [true]',
' -b Base directory to serve files from [/]',
' -i Display autoIndex [true]',
' -g --gzip Serve gzip files when possible [false]',
' -e --ext Default file extension if none supplied [none]',
Expand Down Expand Up @@ -51,6 +52,7 @@ var port = argv.p || parseInt(process.env.PORT, 10),
ssl = !!argv.S || !!argv.ssl,
proxy = argv.P || argv.proxy,
utc = argv.U || argv.utc,
baseDir = argv.b,
logger;

if (!argv.s && !argv.silent) {
Expand Down Expand Up @@ -99,6 +101,7 @@ function listen(port) {
root: argv._[0],
cache: argv.c,
showDir: argv.d,
baseDir: baseDir,
autoIndex: argv.i,
gzip: argv.g || argv.gzip,
robots: argv.r || argv.robots,
Expand All @@ -125,7 +128,8 @@ function listen(port) {
var server = httpServer.createServer(options);
server.listen(port, host, function () {
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
protocol = ssl ? 'https://' : 'http://';
protocol = ssl ? 'https://' : 'http://',
path = baseDir ? '/' + baseDir.replace(/^\//, '') : '';

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

if (argv.a && host !== '0.0.0.0') {
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString())));
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString()) + path));
}
else {
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString()) + path));
}
});
});
Expand All @@ -153,7 +157,7 @@ function listen(port) {
logger.info('Hit CTRL-C to stop the server');
if (argv.o) {
opener(
protocol + canonicalHost + ':' + port,
protocol + canonicalHost + ':' + port + path,
{ command: argv.o !== true ? argv.o : null }
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function HttpServer(options) {
before.push(ecstatic({
baseDir: this.baseDir,
root: this.root,
baseDir: options.baseDir,
cache: this.cache,
showDir: this.showDir,
showDotfiles: this.showDotfiles,
Expand Down
40 changes: 40 additions & 0 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,45 @@ vows.describe('http-server').addBatch({
assert.ok(res.headers['access-control-allow-headers'].split(/\s*,\s*/g).indexOf('X-Test') >= 0, 204);
}
}
},
'When baseDir is specified': {
topic: function () {
var server = httpServer.createServer({
root: root,
baseDir: '/test'
});
server.listen(8083);
this.callback(null, server);
},
'it should serve files at the specified baseDir': {
topic: function () {
request('http://127.0.0.1:8083/test/file', this.callback);
},
'status code should be 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
topic: function (res, body) {
var self = this;
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
self.callback(err, data, body);
});
},
'should match content of served file': function (err, file, body) {
assert.equal(body.trim(), file.trim());
}
}
},
'it should not serve files at the root': {
topic: function () {
request('http://127.0.0.1:8083/file', this.callback);
},
'status code should be 403': function (res) {
assert.equal(res.statusCode, 403);
},
'and file content should be empty': function (res) {
assert.equal(res.body, '');
}
}
}
}).export(module);

0 comments on commit 6201b9b

Please sign in to comment.