3

Node-RED Module for Visual NodeJS Programming

 3 years ago
source link: https://hackernoon.com/node-red-module-for-visual-nodejs-programming-mx3i34j2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Node-RED Module for Visual NodeJS Programming

6
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png

@johnjardinJohn Jardin

https://www.youtube.com/bleedingcode

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Simple NodeJS Express App

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png

server.js

0 reactions
heart.png
light.png
money.png
thumbs-down.png
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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
  • Manually via Terminal
  • Via a Web API
  • Via a schedule

package.json

0 reactions
heart.png
light.png
money.png
thumbs-down.png
{
   "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 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.

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Assuming you’ve watched 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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

That’s it for now. Thanks so much for reading/watching and stay tuned for much more to come.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Cheers :)

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Also published behind a paywallhere.

0 reactions
heart.png
light.png
money.png
thumbs-down.png
6
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png
by John Jardin @johnjardin. https://www.youtube.com/bleedingcodeRead my stories
Join Hacker Noon

Create your free account to unlock your custom reading experience.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK