node-red can be used to quickly convert your shell scripts into functioning APIs.

We all have those scripts that grow over time and does incredibly useful things for internal use. Often, more than the core logic of those, we end up spending time to make emails, batch processing etc.

Useful things usually get to a point where people want to build additional systems on top of these.

APIs that generate JSON are pretty much the default standard for consuming reusable microservices. When concurrent load is controllable like in an intranet, node-red makes it very easy to build APIs. What started as fun project has been proving incredibly powerful and useful for various use cases.

Following is a screenshot tour on how to make a simple bash script that adds n days to current date and gives the new date; to a JSON API.

$cat /tmp/responder.sh
#!/bin/bash
n=${1:-0}
d=`date -d “+$n days”`
echo “{‘n’:$n,’date’:’$d’}”

Getting setup - 2 min

  1. Install node-red
  2. Start node-red server and login to http://localhost:1880/admin/
  3. Read up a bit about creating http end point from cookbook.

Building your flow — 2 min

overall flow

Overall flow

Drag and drop http-in, function, exec and http-out components from left side bar. Connect these. Note that for exec, it has 3 outputs and the top one is the stdout. Use that to connect out.

Now, double click on each of these to configure as below.

Get http-in; url is /dateafter/variable

Get http-in; url is /dateafter/variable

Let us get the parameter n from request and set it as payload

Let us get the parameter n from request and set it as payload

Exec points to our shell script

Exec points to our shell script

Just hit that red button on top right called “Deploy” and you are done! You don’t need to configure http-out node at all.

Let us test drive — 1 min

$ curl http://127.0.0.1:1880/dateafter/6
  {‘n’:6,’date’:’Mon Sep 2 07:27:36 IST 2019'}
$ curl http://127.0.0.1:1880/dateafter/-1
  {‘n’:-1,’date’:’Mon Aug 26 07:27:39 IST 2019'}

Isn’t it easy? Easier than cooking instant noodles :)

Ok so what, we don’t need to have a service to compute dates!

Come on, imagine what all you can do:

  1. For fun, change the script to call psql/mysql with a parameterized SQL that fetches data in JSON format from a db. PostgreSQL comes with JSON functions to convert a query output to json by simply wrapping your query into a sub-query with a function. Do that and give the URL to your super-duper web programmer to build a front end to your APIs!
  2. Let us say you are an architect who needs to design complex flows (“orchestration” — if you want a complex word for this) and get engineers to understand the componentry. Just draw it up here and using the function component, you can even code the pseudo-code inside. Guess what, while proper code is getting built, front-end engineers can use your little node-red server to code the UX assuming you are giving mocked responses.
  3. You want to parse parts of log files and load it into a analytics db from a production server; and you don’t have time to invest in kafka or other such things. Just do a regular cron that greps for patterns in the log files and send it to a node-red API as a post that simply reads the input and a script that appends to the analytics db.

If you go through various components available on default palette on the left; or node-red site, I am sure you can come up with many different use cases for helping your customers and colleagues.

PS: This article was posted in medium.com as well.