Repository: http-party/http-server (language: javascript)

Issue:
Use relative paths in directory listing?
**Steps to reproduce the issue, if applicable. Include the actual command and output and/or stack trace.**

Thanks for http-server.

When serving a folder of files/paths using http-server, while it is being reverse-proxied by NGINX or Apache, the hard-coding of absolute paths (leading "/") in hrefs makes life difficult.

**What did you expect to happen?**

I would expect relative URL paths to the files/folders in the dynamic index output.

**If the issue is a feature request, what is the motivation / use case for it?**

Using Apache or NGINX to proxy is very common. The configuration is relatively simple if the application being proxied does not make assumptions about where it resides. When applications return URLs with leading slashes, the proxy configuration gets much more complicated, error prone, and expensive.

**Tell us about your environment**
- **exact http-server version:**
0.12.3
- **Node version:**
v8.17.0
- **Platform:**
smartos
**Other information (related issues, suggestions for a fix, etc):**



Discussion / hints:
This issue has been inactive for 180 days
I think this is a valid point and switching to relative paths shouldn't interfere with functionality at all

Fix patch:
diff --git a/lib/core/show-dir/index.js b/lib/core/show-dir/index.js
index 70cb5d1bb..f496b2d22 100644
--- a/lib/core/show-dir/index.js
+++ b/lib/core/show-dir/index.js
@@ -95,7 +95,7 @@ module.exports = (opts) => {
           const writeRow = (file) => {
             // render a row given a [name, stat] tuple
             const isDir = file[1].isDirectory && file[1].isDirectory();
-            let href = `${parsed.pathname.replace(/\/$/, '')}/${encodeURIComponent(file[0])}`;
+            let href = `./${encodeURIComponent(file[0])}`;
 
             // append trailing slash and query for dir entry
             if (isDir) {


Test patch:
diff --git a/test/main.test.js b/test/main.test.js
index 0d1c1b26a..cead96470 100644
--- a/test/main.test.js
+++ b/test/main.test.js
@@ -57,8 +57,8 @@ test('http-server main', (t) => {
             requestAsync("http://localhost:8080/").then(res => {
               t.ok(res);
               t.equal(res.statusCode, 200);
-              t.includes(res.body, '/file');
-              t.includes(res.body, '/canYouSeeMe');
+              t.includes(res.body, './file');
+              t.includes(res.body, './canYouSeeMe');
 
               // Custom headers
               t.equal(res.headers['access-control-allow-origin'], '*');
diff --git a/test/showdir-href-encoding.test.js b/test/showdir-href-encoding.test.js
index df5ae4cef..74f5ca7ed 100644
--- a/test/showdir-href-encoding.test.js
+++ b/test/showdir-href-encoding.test.js
@@ -26,7 +26,7 @@ test('url encoding in href', (t) => {
     request.get({
       uri,
     }, (err, res, body) => {
-      t.match(body, /href="\/base\/show-dir%24%24href_encoding%24%24\/aname%2Baplus.txt"/, 'We found the right href');
+      t.match(body, /href="\.\/aname%2Baplus.txt"/, 'We found the right href');
       server.close();
       t.end();
     });
diff --git a/test/showdir-search-encoding.test.js b/test/showdir-search-encoding.test.js
index 9561881c2..bcda82b0b 100644
--- a/test/showdir-search-encoding.test.js
+++ b/test/showdir-search-encoding.test.js
@@ -26,7 +26,7 @@ test('directory listing with query string specified', (t) => {
     request.get({
       uri,
     }, (err, res, body) => {
-      t.match(body, /href="\/base\/subdir\/\?a=1&#x26;b=2"/, 'We found the encoded href');
+      t.match(body, /href="\.\/subdir\/\?a=1&#x26;b=2"/, 'We found the encoded href');
       t.notMatch(body, /a=1&b=2/, 'We didn\'t find the unencoded query string value');
       server.close();
       t.end();
diff --git a/test/showdir-with-spaces.test.js b/test/showdir-with-spaces.test.js
index e5106c406..870eade2d 100644
--- a/test/showdir-with-spaces.test.js
+++ b/test/showdir-with-spaces.test.js
@@ -26,7 +26,7 @@ test('directory listing when directory name contains spaces', (t) => {
     request.get({
       uri,
     }, (err, res, body) => {
-      t.ok(/href="\/base\/subdir_with%20space\/index.html"/.test(body), 'We found the right href');
+      t.ok(/href="\.\/index.html"/.test(body), 'We found the right href');
       server.close();
       t.end();
     });