Add ROOT_URL_PATH parsing

This commit is contained in:
Misha Wolfson 2016-12-23 00:18:44 -06:00
parent 924afec7c2
commit 93ab6efe65
2 changed files with 19 additions and 0 deletions

View file

@ -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: /]'
},
};

View file

@ -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);