From 93ab6efe65bf64ccc6d939019618731c564c58a1 Mon Sep 17 00:00:00 2001 From: Misha Wolfson Date: Fri, 23 Dec 2016 00:18:44 -0600 Subject: [PATCH] Add ROOT_URL_PATH parsing --- lib/arguments.js | 4 ++++ lib/spserver.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/arguments.js b/lib/arguments.js index 95e9d52..685b625 100644 --- a/lib/arguments.js +++ b/lib/arguments.js @@ -41,4 +41,8 @@ module.exports = { alias: 'i', describe: 'IP server runs on [default: 0.0.0.0]' }, + rooturlpath: { + alias: 'r', + describe: 'Root URL path server is deployed on; will be removed from URL when resolving to files [default: /]' + }, }; diff --git a/lib/spserver.js b/lib/spserver.js index fa3e440..f1537b7 100644 --- a/lib/spserver.js +++ b/lib/spserver.js @@ -3,6 +3,7 @@ var fs = require('fs'); var http = require('http'); var path = require('path'); +var url = require('url'); var _ = require('lodash'); var nStatic = require('node-static'); @@ -32,6 +33,8 @@ function _resolveFinalSettings(settings) { finalSettings[field] = settings[field] || settings[env][field]; }); + finalSettings.rooturlpath = config.get('rooturlpath') || config.get('ROOT_URL_PATH') || '/'; + // For 'staticOptions', there are no command-line flags, so individual configuration options // override global defaults where set finalSettings.staticOptions = _.defaultsDeep(settings.staticOptions, settings[env].staticOptions); @@ -66,6 +69,16 @@ function generateBase(file, finalSettings) { }; } +function _rerouteRootUrl(reqUrl, rootUrl) { + var parsedUrl = url.parse(reqUrl); + + parsedUrl.pathname = path.normalize( + parsedUrl.pathname.replace(rootUrl, '/') || '/' + ); + + return url.format(parsedUrl); +} + var spserver = function (settings) { var finalSettings = _resolveFinalSettings(settings); @@ -86,6 +99,8 @@ var spserver = function (settings) { '(' + res.statusCode + ')', 'took', requestTime, 'ms'); }; + req.url = _rerouteRootUrl(req.url, finalSettings.rooturlpath); + res.addListener('finish', done); res.addListener('close', done);