Add ROOT_URL_PATH parsing
This commit is contained in:
parent
924afec7c2
commit
93ab6efe65
2 changed files with 19 additions and 0 deletions
|
@ -41,4 +41,8 @@ module.exports = {
|
||||||
alias: 'i',
|
alias: 'i',
|
||||||
describe: 'IP server runs on [default: 0.0.0.0]'
|
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: /]'
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
var url = require('url');
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var nStatic = require('node-static');
|
var nStatic = require('node-static');
|
||||||
|
@ -32,6 +33,8 @@ function _resolveFinalSettings(settings) {
|
||||||
finalSettings[field] = settings[field] || settings[env][field];
|
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
|
// For 'staticOptions', there are no command-line flags, so individual configuration options
|
||||||
// override global defaults where set
|
// override global defaults where set
|
||||||
finalSettings.staticOptions = _.defaultsDeep(settings.staticOptions, settings[env].staticOptions);
|
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 spserver = function (settings) {
|
||||||
var finalSettings = _resolveFinalSettings(settings);
|
var finalSettings = _resolveFinalSettings(settings);
|
||||||
|
|
||||||
|
@ -86,6 +99,8 @@ var spserver = function (settings) {
|
||||||
'(' + res.statusCode + ')', 'took', requestTime, 'ms');
|
'(' + res.statusCode + ')', 'took', requestTime, 'ms');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
req.url = _rerouteRootUrl(req.url, finalSettings.rooturlpath);
|
||||||
|
|
||||||
res.addListener('finish', done);
|
res.addListener('finish', done);
|
||||||
res.addListener('close', done);
|
res.addListener('close', done);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue