From 415e1c368a7144482234f916c42f1a24ce53bf57 Mon Sep 17 00:00:00 2001 From: Jesse Ditson Date: Mon, 18 Jul 2016 19:37:33 -0700 Subject: [PATCH 1/4] Add baseDir option, add -b flag to cli options to specify base path --- README.md | 2 ++ bin/http-server | 8 ++++++-- lib/http-server.js | 1 + test/http-server-test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 27f2de7b..cdc1e16a 100644 --- a/README.md +++ b/README.md @@ -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`) diff --git a/bin/http-server b/bin/http-server index 926e0dd7..3cdb4dd8 100755 --- a/bin/http-server +++ b/bin/http-server @@ -21,6 +21,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]', @@ -50,6 +51,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) { @@ -97,6 +99,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, @@ -123,7 +126,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), @@ -151,7 +155,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 } ); } diff --git a/lib/http-server.js b/lib/http-server.js index 7e3e06df..bc09c412 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -95,6 +95,7 @@ function HttpServer(options) { before.push(ecstatic({ root: this.root, + baseDir: options.baseDir, cache: this.cache, showDir: this.showDir, showDotfiles: this.showDotfiles, diff --git a/test/http-server-test.js b/test/http-server-test.js index e64f04e1..a8578161 100644 --- a/test/http-server-test.js +++ b/test/http-server-test.js @@ -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); From ed1d639810a78d148f54db5cb330ef11c713d511 Mon Sep 17 00:00:00 2001 From: Jesse Ditson Date: Fri, 22 Jun 2018 15:01:51 -0700 Subject: [PATCH 2/4] add path to logged URLs --- bin/http-server | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/http-server b/bin/http-server index 3cdb4dd8..e01c1091 100755 --- a/bin/http-server +++ b/bin/http-server @@ -136,13 +136,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)); } }); }); From 29e6c6a4e04538a9dc84cf3c2c20886c39aab4e4 Mon Sep 17 00:00:00 2001 From: Ivan Montiel Date: Tue, 5 Sep 2017 10:14:47 -0700 Subject: [PATCH 3/4] Add baseDir option and pass-through to ecstatic Upgrade ecstatic to version 3.0.0. Use ecstatic's baseDir option to set a base directory. See #390 --- bin/http-server | 2 ++ lib/http-server.js | 2 ++ 2 files changed, 4 insertions(+) diff --git a/bin/http-server b/bin/http-server index 926e0dd7..7d9bac9d 100755 --- a/bin/http-server +++ b/bin/http-server @@ -18,6 +18,7 @@ if (argv.h || argv.help) { 'usage: http-server [path] [options]', '', 'options:', + ' -b Base path [/]', ' -p Port to use [8080]', ' -a Address to use [0.0.0.0]', ' -d Show directory listings [true]', @@ -94,6 +95,7 @@ else { function listen(port) { var options = { + baseDir: argv.b, root: argv._[0], cache: argv.c, showDir: argv.d, diff --git a/lib/http-server.js b/lib/http-server.js index 7e3e06df..8d40fe17 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -43,6 +43,7 @@ function HttpServer(options) { this.headers = options.headers || {}; + this.baseDir = options.baseDir === undefined ? '/' : options.baseDir; this.cache = options.cache === undefined ? 3600 : options.cache; // in seconds. this.showDir = options.showDir !== 'false'; this.autoIndex = options.autoIndex !== 'false'; @@ -94,6 +95,7 @@ function HttpServer(options) { } before.push(ecstatic({ + baseDir: this.baseDir, root: this.root, cache: this.cache, showDir: this.showDir, From 1e2b93da09dd3b235d606542ce3c91a7d3a89453 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sat, 23 Jun 2018 14:23:13 +0300 Subject: [PATCH 4/4] Fix merge --- bin/http-server | 2 -- lib/http-server.js | 1 - 2 files changed, 3 deletions(-) diff --git a/bin/http-server b/bin/http-server index 650debe4..e01c1091 100755 --- a/bin/http-server +++ b/bin/http-server @@ -18,7 +18,6 @@ if (argv.h || argv.help) { 'usage: http-server [path] [options]', '', 'options:', - ' -b Base path [/]', ' -p Port to use [8080]', ' -a Address to use [0.0.0.0]', ' -d Show directory listings [true]', @@ -97,7 +96,6 @@ else { function listen(port) { var options = { - baseDir: argv.b, root: argv._[0], cache: argv.c, showDir: argv.d, diff --git a/lib/http-server.js b/lib/http-server.js index ea448994..8d40fe17 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -97,7 +97,6 @@ function HttpServer(options) { before.push(ecstatic({ baseDir: this.baseDir, root: this.root, - baseDir: options.baseDir, cache: this.cache, showDir: this.showDir, showDotfiles: this.showDotfiles,