Hello all, first post.
I'm six months in to using Node Red (what a great package). I don't have a JS background, skill set is IT / networking. I've hit my first stumbling block and wondered if anyone can shed some light.
I need to have admin interface and dashboard secured, while allowing unsecured access to two httpin nodes on two paths. Data will be transmitted two these nodes over SSL, I've set up a self signed cert for this.
From research so far, I need to write some middleware to handle the Auth? I've commented out the sections below in settings.js, I read those paths / routes are higher priority and conflict if enabled:
adminAuth
httpNodeAuth
httpStaticAuth
Middleware code in settings.js:
httpAdminMiddleware: function (req, res, next) {
const basicAuth = require('express').basicAuth({
users: {
'admin': 'hashxxxxxxxxx',
'dashboard': 'hashxxxxxxxxx',
},
challenge: true,
});
// Check if the request is for the admin interface or dashboard
if (req.url.startsWith('/admin') || req.url.startsWith('/ui')) {
// Use basicAuth middleware for admin interface and dashboard authentication
return basicAuth(req, res, next);
} else if (req.url.startsWith('/unsecure-path1')) {
// No authentication needed for the first unsecured path
return next();
} else if (req.url.startsWith('/unsecure-path2')) {
// No authentication needed for the second unsecured path
return next();
}
return next();
},
When I save and reboot and refresh the admin interface, I get the below error. I don't know if it's an error in my code or missing express module. Node v20.5.0, npm v9.8.0 installed. Do I have to install express? I don't know why but I thought it was part of Node Red.
If I have to install it, where does it need to go?
Error:
Error: Cannot find module 'express'
Require stack:
/home/username/.node-red/settings.js
/usr/lib/node_modules/node-red/red.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
at Hook._require.Module.require (/usr/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:81:25)
at require (node:internal/modules/helpers:119:18)
at httpAdminMiddleware (/home/username/.node-red/settings.js:194:23)
at Layer.handle [as handle_request] (/usr/lib/node_modules/node-red/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/usr/lib/node_modules/node-red/node_modules/express/lib/router/index.js:328:13)
at /usr/lib/node_modules/node-red/node_modules/express/lib/router/index.js:286:9
at Function.process_params (/usr/lib/node_modules/node-red/node_modules/express/lib/router/index.js:346:12)
at next (/usr/lib/node_modules/node-red/node_modules/express/lib/router/index.js:280:10)
at cors (/usr/lib/node_modules/node-red/node_modules/cors/lib/index.js:188:7)
Many thanks in advance.
Anders