Skip to content
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

Add --base-dir to cli options to specify base path #837

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This will install `http-server` globally so that it may be run from the command
| ------------- |-------------|-------------|
|`-p` or `--port` |Port to use. Use `-p 0` to look for an open port, starting at 8080. It will also read from `process.env.PORT`. |8080 |
|`-a` |Address to use |0.0.0.0|
|`--base-dir` | Base path to serve files from | `/` |
|`-d` |Show directory listings |`true` |
|`-i` | Display autoIndex | `true` |
|`-g` or `--gzip` |When enabled it will serve `./public/some-file.js.gz` in place of `./public/some-file.js` when a gzipped version of the file exists and the request accepts gzip encoding. If brotli is also enabled, it will try to serve brotli first.|`false`|
Expand Down
13 changes: 9 additions & 4 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (argv.h || argv.help) {
' -p --port Port to use. If 0, look for open port. [8080]',
' -a Address to use [0.0.0.0]',
' -d Show directory listings [true]',
' --base-dir Base directory to serve files from [/]',
' -i Display autoIndex [true]',
' -g --gzip Serve gzip files when possible [false]',
' -b --brotli Serve brotli files when possible [false]',
Expand Down Expand Up @@ -73,6 +74,7 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
proxyOptions = argv['proxy-options'],
utc = argv.U || argv.utc,
version = argv.v || argv.version,
baseDir = argv['base-dir'],
logger;

var proxyOptionsBooleanProps = [
Expand Down Expand Up @@ -142,6 +144,7 @@ function listen(port) {
cache: argv.c,
timeout: argv.t,
showDir: argv.d,
baseDir: baseDir,
autoIndex: argv.i,
gzip: argv.g || argv.gzip,
brotli: argv.b || argv.brotli,
Expand Down Expand Up @@ -197,7 +200,8 @@ function listen(port) {

var server = httpServer.createServer(options);
server.listen(port, host, function () {
var protocol = tls ? 'https://' : 'http://';
var protocol = tls ? 'https://' : 'http://',
path = baseDir ? '/' + baseDir.replace(/^\//, '') : '';

logger.info([
chalk.yellow('Starting up http-server, serving '),
Expand All @@ -216,18 +220,19 @@ function listen(port) {
([chalk.yellow('AutoIndex: '), argv.i ? chalk.red('not visible') : chalk.cyan('visible')].join('')),
([chalk.yellow('Serve GZIP Files: '), argv.g || argv.gzip ? chalk.cyan('true') : chalk.red('false')].join('')),
([chalk.yellow('Serve Brotli Files: '), argv.b || argv.brotli ? chalk.cyan('true') : chalk.red('false')].join('')),
([chalk.yellow('Default File Extension: '), argv.e ? chalk.cyan(argv.e) : (argv.ext ? chalk.cyan(argv.ext) : chalk.red('none'))].join(''))
([chalk.yellow('Default File Extension: '), argv.e ? chalk.cyan(argv.e) : (argv.ext ? chalk.cyan(argv.ext) : chalk.red('none'))].join('')),
([chalk.yellow('Base directory: '), baseDir ? chalk.cyan(baseDir) : chalk.cyan('/')].join(''))
].join('\n'));

logger.info(chalk.yellow('\nAvailable on:'));

if (argv.a && host !== '0.0.0.0') {
logger.info(` ${protocol}${host}:${chalk.green(port.toString())}`);
logger.info(` ${protocol}${host}:${chalk.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 + ':' + chalk.green(port.toString())));
logger.info((' ' + protocol + details.address + ':' + chalk.green(port.toString()) + path));
}
});
});
Expand Down
1 change: 1 addition & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function HttpServer(options) {

before.push(httpServerCore({
root: this.root,
baseDir: options.baseDir,
cache: this.cache,
showDir: this.showDir,
showDotfiles: this.showDotfiles,
Expand Down
31 changes: 31 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,37 @@ test('http-server main', (t) => {
}
});
}),

new Promise((resolve) => {
const server = httpServer.createServer({
root,
baseDir: '/test'
});

server.listen(8084, async () => {
try {
await Promise.all([
// should serve files at the specified baseDir
requestAsync("http://localhost:8084/test/file").then(async (res) => {
t.equal(res.statusCode, 200);
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim());
}).catch(err => t.fail(err.toString())),

// should not serve files at the root
requestAsync("http://localhost:8084/file").then((res) => {
t.equal(res.statusCode, 403);
t.equal(res.body, '');
}).catch(err => t.fail(err.toString())),
]);
} catch (err) {
t.fail(err.toString());
} finally {
server.close();
resolve();
}
});
}),
]).then(() => t.end())
.catch(err => {
t.fail(err.toString());
Expand Down