Help - Middleware for httpin node Auth

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

Have you tried installing express using npm install -g express and trying things out again?

If you have installed express and still getting the error try setting the following in the settings.js file:

functionGlobalContext: {
        express:require("express")
    },

and then try it out.

Shan, thanks for the quick reply. I haven't installed express, the only modules I installed previously were all done via the manage pallete menu on the GUI.

I'll give that a go, do I have to install express in a specific location or the standard path the installer uses?

Cheers

Packages like that would need to be installed in your userDir (normally ~/.node-red).

Try:

cd ~/.node-red
npm install express

(or might be expressjs, I can never remember).

Don't use the -g suggested by Shan as that will install it globally and cause other issues.

Some progress, express error has gone and replaced with this one.

TypeError: require(...).basicAuth is not a function

I thought I'd named the module wrong, I updated to the below but the error just says Error: Cannot find module 'express-basic-auth'

 const basicAuth = require('express-basic-auth')({

Do I need to install this auth module too?

Cheers

Yes. :slight_smile:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.