DEV Community

loading...
Cover image for Visual NodeJS Programming Using Node-RED

Visual NodeJS Programming Using Node-RED

johnjardincodes profile image 💻 JOHN JARDIN | Bleeding Code ・4 min read

In this article I'm going to introduce you to a NodeJS module that allows you to create and deploy server-side processes by using a visual, drag n drop style interface in your Web Browser. The module I'm referring to is called Node-RED: A flow-based programming tool that allows you to design processes (aka Flows) by wiring together microservices.

Watch The Video on YouTube

Created in 2013, Node-RED was initially intended for visually wiring together the Internet of Things, but as it matured, it evolved into something way more powerful, enough to be deployed as middleware within enterprise production environments. The power behind Node-RED is how it hides boilerplate code from the design interface, allowing you to quickly build and deploy processes and integrations.

To demonstrate this, I'm going to compare a simple Node App with a Node-RED Flow, which will show you the time saving to be had and why you should be excited to learn this tech:

Simple NodeJS Express App

The code below is for a simple Express application that outputs Hello World.

server.js

const Express = require('express')
const App = Express()
const HTTP = require('http')
const Cron = require('node-cron')
const Functions = require('./functions')

// Create Route
App.get('/api/greet', (req, res) => {
   const result = Functions.greet()
   res.send(result)
})

// Start Server
const port = 6025
HTTP.createServer(App).listen(port, () => {
   console.log('NodeJS App Listening on: ', port)

   // Schedule CRON Job
   Cron.schedule('*/5 * * * * *', () => {
      Functions.greet()
   })
})
Enter fullscreen mode Exit fullscreen mode

functions.js

const greet = () => {
   const result = 'Hello World'
   console.log(result)
   return result
}
exports.greet = greet
Enter fullscreen mode Exit fullscreen mode

In server.js, we've got some logic for our Express server, a scheduled process, as well as an endpoint called /api/greet. We then have a functions.js file that exports a greet() function, which returns the text Hello World. The CRON job in server.js runs every 5 seconds, triggering the greet() function on every run. This function is also triggered by the /api/greet endpoint.

So the point of this exercise is that we're going to trigger the greet() function using 3 events:

  • Manually via Terminal
  • Via a Web API
  • Via a schedule

package.json

{
   "name": "node-red-intro-nodejs-app",
   "version": "0.0.1",
   "description": "Node-RED Intro - NodeJS App",
   "main": "server.js",
   "scripts": {
      "manual": "node -e \"require('./functions').greet()\"",
      "endpoint": "curl http://localhost:6025/api/greet"
   },
   "engines": {
      "node": "12.18.4"
   },
   "author": "Agilit-e",
   "license": "MIT",
   "dependencies": {
      "express": "4.17.1",
      "node-cron": "3.0.0"
   }
}
Enter fullscreen mode Exit fullscreen mode
  • We trigger the function manually by requiring the functions.js in terminal or command prompt, and executing the greet() function. You can see I've added this as a manual script in package.json, which I will trigger by running npm run manual. This will write Hello World as a response to the console.
  • For our next test we start the NodeJS server and trigger the greet() function via an HTTP request. Our endpoint will be http://127.0.01:6025/api/greet. Because it's a simple GET request, we can just use curl command in Terminal or alternatively open the URL in a browser window. For ease of execution, I have this also as a script in package.json, which I will trigger using npm run endpoint. You can see Hello World is returned as a response.
  • Finally, we can also see that in the background, the scheduled CRON job is printing the response to the console every 5 seconds.

Now, excluding the time that was taken to set up the base of this project, i.e. the package.json, dependencies and HTTP server…creating the HTTP endpoint, the greet() function and the CRON job will take you a couple of minutes if you know what you're doing. For fun, let's see how fast we can achieve this in Node-RED:

NOTE: For the Node-RED demo, click here to watch my video on YouTube or alternatively watch the embedded video above (Fast Forward to minute 2:42). Because we're still at the beginning stages of my Node-RED Series and this is more an introduction article, it will be tough to explain what I'm doing in Node-RED in writing. Apologies for any inconvenience caused.

Assuming you've watched the the video demo, I trust you enjoyed the fun comparison of native NodeJS and Node-RED. What can take minutes in NodeJS can be achieved in a fraction of the time using Node-RED. Scale that up to a much bigger project and you're going to see a massive time saving.

Now this is the first article of my Up and Running with Node-RED series, with many more to come that will focus on all areas of Node-RED, from basic to advanced functionality, to real world scenarios. If you're not yet subscribed or following me, now would be a great time to do so, so that you're notified when new content is released.

I want to close off by providing you with a reference to resources that will help you learn more about Node-RED:

Your first stop will be Node-RED's website - nodered.org. This site will give you a lot of insight into what Node-RED is, how it works, and provides proper end to end documentation on how to achieve almost anything you want with it. You can find almost anything you need regarding Node-RED, including links to online communities and forums which can be found at the bottom of the home page.

Finally, I highly recommend you subscribe to the Node-RED channel on YouTube, managed by the creators of Node-RED. It includes a number of tutorials as well as live webinars and streams.

That's it for now. Thanks so much for reading/watching and stayed tuned for much more to come.
Cheers :)

Discussion (4)

Collapse
imthedeveloper profile image
ImTheDeveloper

Great example of using node red!

I was actually one of the very early adopters when it was in alpha/beta and looking back to where we are now it's really held onto its principles and excellent continued work by Nick and Dave to bring it to where it is today.

I originally used node-red for the raspberry pi integrations and IoT devices, but I still drop it into some of my projects now for very quick prototyping and doing anything where I feel writing code is going to be manual effort spent better elsewhere.

Have to say it still feels like cheating when you can produce a flow in a few minutes that would take hours of setup outside the environment.

dev.to/imthedeveloper/comment/1lao Some of the old projects all using node red for integration!

Collapse
johnjardincodes profile image
💻 JOHN JARDIN | Bleeding Code Author

Thanks very much for reading and for the comment 👍. That's awesome I'm also an early adopter and took it on when it was released to IBM Cloud (formerly Bluemix).

By 2014 I was presenting Node-RED at events. The big difference with me though is that I went a completely different route with Node-RED. I actually use it as middleware for business application development and integration. I hardly use it for IoT.

Fun thing is, I have Node-RED running at multiple clients, running production flows and doing a brilliant job at that.

Will definitely check out your projects 😎.

Collapse
imthedeveloper profile image
ImTheDeveloper

Very interesting! Have you come up against any issues running it in such an environment?

Usually we are used to seeing businesses reaching for mulesoft, IBM middleware, apigee, microservices in java etc.

I think the simplicity of the interface catches people off guard with node-red and see it more as a toy/prototyping project so jumping to production at enterprise may not be something they think about.

Thread Thread
johnjardincodes profile image
💻 JOHN JARDIN | Bleeding Code Author

Good question. Although I must say I don't experience much resistance when sugggesting Node-RED. However, my clients most of the time trust me enough to make these calls.

When I need to convince dev teams, it gets trickier, but even then with a few workshops they are ultimately sold to add Node-RED as a utility tool to their environment.

Forem Open with the Forem app