Using Node-RED to mock out RESTful webservices
You are developing an application with a REST architecture and you want to focus on writing the client side without having to set up a database and write the APIs? You are a frontend developer and don’t want to wait for the backend developers to test your application? Or you just want to rapidly create a prototype of your application to show to your customers? Then, Node-RED might be the tool you’re looking for in your next project.
What is Node-RED?
“Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.” – http://nodered.org
Node-RED is a visual tool built on Node.js that you can use to rapidly create APIs, http endpoints… Node-RED is a creation of IBM Emerging Technology and has been developed as an open-source project on GitHub, under the Apache 2 license.
How does it work?
Node-RED provides a browser-based flow editor where users drag-and-drop nodes to represent the web services that have to be connected.
Further nodes can be placed in between the previous nodes to
- perform functions that introduce a delay in the flow (delay node),
- route messages based on their properties (switch node),
- transform the data based on the code you write (function node),
The flow created can then be deployed in a single-click.
This flow is also stored in a JSON file which can be easily shared amongst the developers of your project using your favourite version control system (Git, Subversion…)
Using Node-RED to mock out RESTful webservices
Construct a simple flow
The best way to understand is to show an example of what Node-RED can do. So, we will create our first flow which is a REST endpoint returning the information of the authenticated user in JSON. Once Node-RED is installed and running, you can access the editor at http://localhost:1880 (by default) via your browser. Below is a picture of the editor with the flow:
Creating a simple REST endpoint with Node-RED can be easily done in 6 steps (illustrated in the above screenshot):
- Create a representation of the response in JSON on your file system (this step doesn’t require Node-RED)
- In Node-RED, use the HTTP input node that allows the creation of web services. Then, edit the method (“GET”) and the URL (“/rest/user”). They must match your request.
- Use the file storage node that reads a file and sends its content in the payload of the message. Then, edit the filename which represents the path of the file you have created in the first step.
- Use the JSON function node that converts the message to JSON.
- Use the HTTP response node that sends the message back to the HTTP request received from the HTTP input node defined in step 2.
- Deploy the newly created flow and access the URL you have defined in step 2. You can see the JSON response in your browser.
Modify the flow
The goal of this second example is to place a function in between the nodes of the first examples. This function will allow us to modify the message of the response by adding some headers. We will also see how we can debug a Node-RED flow.
Perform the following operations:
- To debug the flow and see the content of our headers in the message before sending the HTTP response, you can add a debug node. Then, edit the output to debug the property ‘headers’.
- Deploy the changes and access the URL (as previously). You can see the JSON response in your browser and the headers you have added (picture below).
- You can also see the value of your headers in the debut tab of the editor.
More complex flow – testing your application with different types of user
In our last example, we will create a more complex flow. The objective of this flow is to test our application with different types of user (user and administrator in this case) and also to test different scenarios (how does my application behave if the call to the endpoint failed?).
The switch between the different cases is easy as a simple click and doesn’t require to modify the JSON file (which is not a proper way to test your application). It doesn’t even require to redeploy Node-RED.
To achieve such a process, we will require 3 flows (picture below):
The first flow (Part A) is used to inject a pre-defined value by pressing a button in the Node-RED IDE. This value represents the role we want the subsequent HTTP requests to hold (Administrator or User) and is stored in the NodeJS server memory in a store called “global context”. The global context is shared by, and accessible to all nodes. It means that the variable containing the role is now accessible by the other nodes of the application. To test your application as an administrator, you simply should click on the button on the left of the inject node. You don’t have to redeploy Node-RED.
- Create inject nodes based on the number of cases you want to test. Then, edit each node and add the value (describing the role) you want to inject.
- Add a function node. Then, write a function that will store the injected valued in global context.
There isn’t any new stuff in the second flow (Part B).
We just add:
- two new entries for the administrator (you should create a new JSON file)
- the error (add a function node that modify the status code of the response).
The third flow (Part C) represents the entry point of our API (accessible via “GET /rest/user”). It retrieves the role injected in the global context (Part A)and calls the corresponding flow (depending on the role) in Part B.
The first node is the HTTP input node which is the entry point of our API.
- Add a function node and write a function that retrieve the role injected from the global context. If no role has been injected, define the default one.
- Add a switch node that will reroute to the correct action the based on the role injected.
- Create the HTTP request that match with the HTTP input node of the Part B.
As you can see in the above-mentioned examples, Node-RED allows to isolate the frontend developers from the backend dependencies by implementing REST APIs, as mocks. This separation enhances the frontend developer’s performance because they can focus on writing frontend code without deploying real backend servers. But this separation has also a cost: setting up the environment, writing the flows and the mocked data, which may take some time.
Let’s finish with some other uses of Node-RED that might interest you:
- Node-RED can run on a Raspberry Pi or an Arduino;
- Node-RED is not only designed to interact with HTTP but it can also interact with TCP, UDP, WebSocket, MQTT and serial ports;
- Node-RED community is very active: developers has released a repository of various nodes, to interact with numerous services, such as Google APIs for instance
Article written by Martin V.