summaryrefslogtreecommitdiffstats
path: root/lib/cli
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-05-27 12:01:28 +0200
committerSamy Pessé <samypesse@gmail.com>2016-05-27 12:01:28 +0200
commit5f3de0876fd33f77df0eb3ccccd1f964e3a811bc (patch)
tree4e9a0a43e4219adbaeb162c8e7c75ff2c4c9f11a /lib/cli
parent2ceab04d01c32f04f8f6b8a171a563a617c7bb04 (diff)
downloadgitbook-5f3de0876fd33f77df0eb3ccccd1f964e3a811bc.zip
gitbook-5f3de0876fd33f77df0eb3ccccd1f964e3a811bc.tar.gz
gitbook-5f3de0876fd33f77df0eb3ccccd1f964e3a811bc.tar.bz2
Add header 'X-Current-Location' to serve and fix handling of directories
Diffstat (limited to 'lib/cli')
-rw-r--r--lib/cli/server.js23
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/cli/server.js b/lib/cli/server.js
index 555bbb7..752f867 100644
--- a/lib/cli/server.js
+++ b/lib/cli/server.js
@@ -71,11 +71,18 @@ Server.prototype.start = function(dir, port) {
// Redirect to directory's index.html
function redirect() {
+ var resultURL = urlTransform(req.url, function(parsed) {
+ parsed.pathname += '/';
+ return parsed;
+ });
+
res.statusCode = 301;
- res.setHeader('Location', req.url + '/');
- res.end('Redirecting to ' + req.url + '/');
+ res.setHeader('Location', resultURL);
+ res.end('Redirecting to ' + resultURL);
}
+ res.setHeader('X-Current-Location', req.url);
+
// Send file
send(req, url.parse(req.url).pathname, {
root: dir
@@ -106,4 +113,16 @@ Server.prototype.start = function(dir, port) {
});
};
+/**
+ urlTransform is a helper function that allows a function to transform
+ a url string in it's parsed form and returns the new url as a string
+
+ @param {String} uri
+ @param {Function} fn
+ @return {String}
+*/
+function urlTransform(uri, fn) {
+ return url.format(fn(url.parse(uri)));
+}
+
module.exports = Server;