1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add support for multiple static paths with root

closes #3510
This commit is contained in:
Steve-Mcl 2022-04-22 17:06:40 +01:00
parent a7932da207
commit 22000f10df
3 changed files with 66 additions and 7 deletions

View File

@ -207,8 +207,12 @@ function start() {
if (settings.readOnly){
log.info(log._("settings.readonly-mode"))
}
if (settings.httpStatic) {
log.info(log._("runtime.paths.httpStatic",{path:path.resolve(settings.httpStatic)}));
if (settings.httpStatic && settings.httpStatic.length) {
for (let si = 0; si < settings.httpStatic.length; si++) {
let p = path.resolve(settings.httpStatic[si].path);
let r = settings.httpStatic[si].root || "/";
log.info(log._("runtime.paths.httpStatic",{path:`${p} > ${r}`}));
}
}
return redNodes.loadContextsPlugin().then(function () {
redNodes.loadFlows().then(redNodes.startFlows).catch(function(err) {});

View File

@ -298,6 +298,26 @@ httpsPromise.then(function(startupHttps) {
settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
}
if(settings.httpStatic) {
settings.httpStaticRoot = formatRoot(settings.httpStaticRoot || "/");
const statics = Array.isArray(settings.httpStatic) ? settings.httpStatic : [settings.httpStatic];
const sanitised = [];
for (let si = 0; si < statics.length; si++) {
let sp = statics[si];
if(typeof sp === "string") {
sp = { path: sp, root: "" }
sanitised.push(sp);
} else if (typeof sp === "object" && sp.path ) {
sanitised.push(sp);
} else {
continue;
}
sp.subRoot = formatRoot(sp.root);
sp.root = formatRoot(path.posix.join(settings.httpStaticRoot,sp.subRoot));
}
settings.httpStatic = sanitised.length ? sanitised : false;
}
// if we got a port from command line, use it (even if 0)
// replicate (settings.uiPort = parsedArgs.port||settings.uiPort||1880;) but allow zero
if (parsedArgs.port !== undefined){
@ -390,12 +410,28 @@ httpsPromise.then(function(startupHttps) {
if (settings.httpNodeRoot !== false) {
app.use(settings.httpNodeRoot,RED.httpNode);
}
// if (settings.httpStatic) {
// settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
// if (settings.httpStaticAuth) {
// app.use("/",basicAuthMiddleware(settings.httpStaticAuth.user,settings.httpStaticAuth.pass));
// }
// app.use("/",express.static(settings.httpStatic));
// }
if (settings.httpStatic) {
settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
if (settings.httpStaticAuth) {
app.use("/",basicAuthMiddleware(settings.httpStaticAuth.user,settings.httpStaticAuth.pass));
let appUseMem = {};
for (let si = 0; si < settings.httpStatic.length; si++) {
const sp = settings.httpStatic[si];
const filePath = sp.path;
const thisRoot = sp.root || "/";
if(appUseMem[filePath + "::" + thisRoot]) {
continue;// this path and root already registered!
}
appUseMem[filePath + "::" + thisRoot] = true;
if (settings.httpStaticAuth) {
app.use(thisRoot, basicAuthMiddleware(settings.httpStaticAuth.user, settings.httpStaticAuth.pass));
}
app.use(thisRoot, express.static(filePath));
}
app.use("/",express.static(settings.httpStatic));
}
function getListenPath() {

View File

@ -137,6 +137,8 @@ module.exports = {
* - httpNodeCors
* - httpNodeMiddleware
* - httpStatic
* - httpStaticRoot
* - httpStaticCors
******************************************************************************/
/** the tcp port that the Node-RED web server is listening on */
@ -218,8 +220,25 @@ module.exports = {
/** When httpAdminRoot is used to move the UI to a different root path, the
* following property can be used to identify a directory of static content
* that should be served at http://localhost:1880/.
* When httpStaticRoot is set differently to httpAdminRoot, there is no need
* to move httpAdminRoot
*/
//httpStatic: '/home/nol/node-red-static/',
//httpStatic: '/home/nol/node-red-static/', //single static source
/* OR multiple static sources can be created using an array of objects... */
//httpStatic: [
// {path: '/home/nol/pics/', root: "/img/"},
// {path: '/home/nol/reports/', root: "/doc/"},
//],
/**
* All static routes will be appended to httpStaticRoot
* e.g. if httpStatic = "/home/nol/docs" and httpStaticRoot = "/static/"
* then "/home/nol/docs" will be served at "/static/"
* e.g. if httpStatic = [{path: '/home/nol/pics/', root: "/img/"}]
* and httpStaticRoot = "/static/"
* then "/home/nol/pics/" will be served at "/static/img/"
*/
//httpStaticRoot: '/static/',
/*******************************************************************************
* Runtime Settings