Using Node-Red with Passport

1,509 views
Skip to first unread message

Brett Forbes

unread,
Dec 6, 2015, 9:31:50 PM12/6/15
to Node-RED
Hi,

I am a newbie, and can I first of all express my admiration for the developers of Node-Red. Well done! Tremendous work, you have taken a big step forward in democratising the development of simple programs for IOT or data management (e.g. getting data from feeds, analysing it and sending the results somewhere).

Secondly, I might point out that I am one of those high-level business-engineering types that Node-Red was designed to attract, not a software engineer, so I apologise in advance if my question seems stupid or obvious.

My application is in social media and I need to be able to access many different social feeds and then send them towards Watson for analysis. Obviously, including the Passport middleware in the application would enable me to authenticate against many different applications, rather than having to code up every individual application using a model similar to the Twitter node.

Newbie Question: Can I use the Passport library in Node-Red? If so, does anyone have any examples of how this can be achieved?

I would greatly appreciate any pointers or assistance.

thanks once again to the developers of this excellent system.

Brett

Julian Knight

unread,
Dec 8, 2015, 4:26:23 AM12/8/15
to Node-RED
I don't currently have an example I'm afraid as I've worked around the issues for my home automation setup - e.g. I don't expose Node-Red to the outside world. Instead I use MQTT as a message hub that is easily integrated elsewhere and is relatively easy to secure & I use the public instance of EmonCMS for the public end of my sensor tracking.

Havinmg said that, it should certainly be possible to do this but not, I'm afraid, using the "standard" installation. Node-Red is well constructed though and the guys have already thought about alternative uses and so support the "embedded" mode installation that is briefly described in the documentation. This is, in fact, how I run NR anyway as it gives me somewhat better control over what is happening and, in particular, allows me to run different versions of NR on the same platform at the same time.

In embedded form, you set up your own Express server so it should be possible to use Passport as Express middleware in conjunction with NR.

I'm on a train at the moment so I don't have access to my server.js file that shows the Express setup. However, I have posted it to this group in the past I think so you may be able to find it. If not, let me know and I will re-post it when I get back to my home office.

Brett Forbes

unread,
Dec 8, 2015, 6:16:17 PM12/8/15
to Node-RED
Hi Julian,

Thanks so much for your response, I appreciate it.

Unfortunately, I am an IBM Bluemix customer and I don't know if I can run Node-Red in an embedded mode. In essence, one can create a Node-Red program locally, and upload it to the Bluemix system if one wants, or alternatively just develop it on the Bluemix cloud. I am not enough of an expert to know if one can create an embedded instance on the node.js runtime on Bluemix, although I suspect that you can.

I would really appreciate it if you could post your server.js file when you get the chance so I can get my team to check it out this "embedded mode".

Thanks once again for your response.
Cheers
B

Nicholas O'Leary

unread,
Dec 8, 2015, 6:49:00 PM12/8/15
to Node-RED

Brett,

I can reply more fully in the morning, but you can certain run whatever you want in Bluemix.

I'm currently working on improving the embedding docs as part of our next release, but a minimal example is already available here: http://nodered.org/docs/embedding.html

Nick


--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Knight

unread,
Dec 9, 2015, 12:29:01 PM12/9/15
to Node-RED
No problem. Long day yesterday (16 1/2 hours!) so I didn't have time to grab anything, I'll try tonight.

Julian Knight

unread,
Dec 9, 2015, 4:20:01 PM12/9/15
to Node-RED
OK, here they are. A package.json and server.js for running a stand-alone, embedded version of NR 0.12+ Note that I'm using Node.js 0.10.* at present because I'm also running an older version of NR until I can get everything moved over to the new one. I'll then move to a newer version of Node.JS which have started directly supporting ARM builds again.

package.json:
{
 "name": "nr-live",
 "version": "1.12.2",
 "description": "Live Node-Red",
 "readme": "na",
 "contributors": [
   {
     "name": "Julian Knight"
   }
 ],
 "repository": "na",
 "author": "jk",
 "license": "MIT",
 "main": "server.js",
 "config": {
"localPort": "8081",
   "rmtPort": "8081"
 },
 "scripts": {
   "start": "node server.js",
   "admin": "node-red-admin ",
   "patch": "npm version patch -m \"Bumped to %s\" ",
   "minor": "npm version minor",
   "major": "npm version major"
 },
 "devDependencies": {},
 "optionalDependencies": {},
 "dependencies": {
   "express": "4.*",
   "lodash": "*",
   "mongodb": "*",
   "node-red": "0.12.*",
   "node-red-contrib-bigtimer": "1.*",
   "node-red-contrib-duskdawn": "1.*",
   "node-red-contrib-esplogin": "1.*",
   "node-red-contrib-grove": "git+https://bitbucket.org/scargill/grove.git",
   "node-red-contrib-later": "0.*",
   "node-red-contrib-rfxcom": "*",
   "node-red-contrib-thermostat": "1.*",
   "node-red-contrib-ui": "1.*",
   "node-red-node-forecastio": "^0.1.3",
   "node-red-node-mongodb": "*",
   "node-sleep": "*"
 },
 "engines": {
   "node": "0.10.*"
 },
 "config": {
   "engine-strict": true
 }
}

server.js:
/*jshint devel: true, node: true*/
/*global: */
"use strict";  /* always for Node.JS, never global in the browser */
var http    = require('http'),
   path    = require("path"),     // Node core library. path library for cross-platform file system specs
   fs      = require("fs"),       // Node core library. Filing system library for access to files & folders on the OS
   express = require("express"),  // THE std library for serving HTTP
       //sleep   = require("sleep"),      // Module to implement blocking sleep() for accurate timing
   RED     = require("node-red"),
   _       = require("lodash")   // lodash (improved underscore) for JS utility fns
;
// Config vars - NB: LAN details are LOCAL. If accessing from the WAN (Internet), use the WAN details instead
var mqttSrv    = '192.168.1.2',  // The LAN address for the MQTT server
   mqttWsPort = '9001',           // The LAN port for the websocket interface to the MQTT server
   nrSrv      = '192.168.1.2',  // The LAN address for this Node-Red server
    // The LAN port for this systems web interface - picked up from env, package.json or fixed value
   nrPort     = process.env.LOCALPORT || process.env.npm_package_config_localPort || '8081'
;
var wanMqttSrv    = '192.168.1.2',  // The WAN address for the MQTT server
   wanMqttWsPort = '9001',           // The WAN port for the websocket interface to the MQTT server
   wanNrSrv      = '192.168.1.2',  // The WAN address for this Node-Red server
 // The WAN port for this systems web interface - picked up from env, package.json or fixed value
   wanNrPort     = process.env.RMTPORT || process.env.npm_package_config_rmtPort || '8081'
;
// Create an Express app
var app = express();
// Add a simple route for static content served from './public'
app.use( "/", express.static("public") );
// Create a server
var httpServer = http.createServer(app);
// Add static route for bower components
app.use( '/bower_components', express.static( path.join(__dirname, '/bower_components') ) );
// Create the settings object - see default settings.js file for other options
var nrSettings = {
   httpAdminRoot:"/red",    // Access the admin web i/f from http://<nrSrv>/red
   httpNodeRoot: "/",               // Access other NR served pages from http://<nrSrv>/
   //uiPort: 1880, // stand alone only
   userDir: path.join('.', '.nodered'),  // default: $HOME/.node-red
   nodesDir: path.join('.', 'nodes'),    // adds extra locn, defaults are userDir/nodes & node-red/nodes
   verbose: true,                                               // For better debugging
   debugMaxLength: 1000,                // max length of debug output
   paletteCategories: [ // Reorder the node pallette
       'subflows', 'input', 'output', 'function', 'storage', 'advanced', 'formats', 'Raspberry Pi', 'social', 'analysis'
   ],
   functionGlobalContext: { // enables and pre-populates the context.global variable
       // os:require('os'),
       // arduino:require('duino') // directly control Arduino's over serial, https://www.npmjs.com/package/duino
       // -- Pass config variables into NR for reference -- //
       'mqttSrv'       : mqttSrv,
       'mqttWsPort'    : mqttWsPort,
       'nrSrv'         : nrSrv,
       'nrPort'        : nrPort,
       'wanMqttSrv'    : wanMqttSrv,
       'wanMqttWsPort' : wanMqttWsPort,
       'wanNrSrv'      : wanNrSrv,
       'wanNrPort'     : wanNrPort,
       // -- Pass in Libraries for convenience in function nodes -- //
       'path'          : path,      // path library for cross-platform file system specs
       'fs'            : fs,        // filing system library for access to files & folders on the OS
       '_'             : _            // lodash (improved underscore) for JS utility fns
},
     // Add a custom middleware function for Express in front of all http in nodes.
 // Allows custom authentication for all http in nodes, or any other sort of common request processing.
   //httpNodeMiddleware: function(req,res,next) {
   //   // Handle/reject the request, or pass it on to the http in node
   //   // by calling next();
   //   next();
   //},
 logging : { console : { level: "info", metrics: false } }
};
// Not used in embedded mode: uiHost, uiPort, httpAdminAuth, httpNodeAuth, httpStatic, httpStaticAuth, https
// Initialise the runtime with a server and settings
RED.init( httpServer, nrSettings );
// Serve the editor UI from /red
app.use( nrSettings.httpAdminRoot, RED.httpAdmin );
// Serve the http nodes UI from /api
app.use( nrSettings.httpNodeRoot, RED.httpNode );
httpServer.listen( nrPort, function(){
 console.log('Express 4 server listening on port %s, serving node-red', nrPort);
});
// Start the runtime
RED.start();

Put those two files into their own folder then run "npm install". Use "npm start" to get everything going.

Note that I'm not restricting the max-old-space as I'm on a Pi 2 which has 1GB of RAM so seems to have no memory issues. If you are using Bluemix with limited memory, you might need to add that parameter - not sure or maybe it does it for you. I expect Nick or Dave will let us know. I'm more a Microsoft Azure man.

There's quite a bit of stuff in there that you can strip out if you don't need it. It's a little overkill but it works on multiple platforms if I need it too which is nice.

With this config, the user directory is in the same folder as these files and is called: "./.nodered" - I find this easier to work with than having it in the root of the running users home folder and it again facilitates running multiple versions of NR simultaneously as I am doing at present. I've also found it sensible to be able to run a copy of your setup under a new version of NR for testing so that you can instantly roll back if it doesn't work.

For the web interfaces. The admin UI is on "/red", anything else starts from "/". There is also a "./public" folder (served on "/") for static files and in case you are using front-end files installed via Bower, they will be accessible in the browser from "/bower_components/...".

Feel free to ask questions, make suggestions or ignore at will! Hope you find these useful.

Regards, Julian.

Julian Knight

unread,
Dec 9, 2015, 4:22:18 PM12/9/15
to Node-RED
Oh, perhaps I should also have said that I auto-start all this on reboot of the Pi using a systemd script that I can also share if needed though probably not of interest to you on Bluemix.

Brett Forbes

unread,
Dec 9, 2015, 6:02:08 PM12/9/15
to Node-RED
Hi Julian,

You've been a true legend!!!

Thanks so much,

Brett


Message has been deleted

Stefania Kaczmarczyk

unread,
Feb 16, 2016, 3:17:10 PM2/16/16
to Node-RED
So I know I didn't originally ask the question, but this post just saved my day.  I really was struggling trying to migrate the Bluemix boilerplate version of Node-RED to an Express driven version and your layout REALLY helped.  I had to change a large chunk to match the bluemix-settings.js parameters but otherwise it worked like a charm.

Thank you soooooo much!!! Like so much.  So so so so much.  I'd give you my first born but I don't have one.

Julian Knight

unread,
Feb 18, 2016, 2:00:55 AM2/18/16
to Node-RED
No problem, we are always happy to help in this group. No need for first-born's, I already have one anyway and he's enough of a handful thanks!
Reply all
Reply to author
Forward
0 new messages