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()
})
})
functions.js
const greet = () => {
const result = 'Hello World'
console.log(result)
return result
}
exports.greet = greet
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"
}
}
- 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 usingnpm 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 :)