Fetch data with GET
In this section, you will learn how to retrieve data using the GET method.
Before working on CRUD operations, ensure your Node-RED environment is connected to your MongoDB database.
To build the GET method, we will need the following nodes:
| Node | Description |
|---|---|
| http in | Listens to HTTP requests. |
| function | Allows to write custom JavaScript code to manipulate data passed through it. |
| mongodb in | Allows retrieving and manipulating data from a MongoDB collection. |
| http response | Replies to HTTP requests. |
-
Drag and drop the nodes onto the dashboard in
the specified order:

-
To connect a node to the next one, click on
the gray square on its right border and drag your mouse to the gray square
on the left border of the next node. You will see a line linking the two
nodes. Now, we are ready to configure each of the nodes.

-
http in
Double-click the node to open its properties. Set the method to GET and name the endpoint for your URL, then click Done.

-
function
Give your function a name to help you navigate your back-end.
To specify that you want to retrieve all the available data, set msg.payload to an empty object. You can do this by copying the following code into the beginning of the function's body:
msg.payload = {};Your function should now look like this:

-
mongodb in
Enter the collection name and select find as the operation. Then, click on Done.

-
http response
Leave the node without any changes.
- Deploy the service.
The API can now retrieve data from your database using the GET method.
To test your REST API, you can use a platform like Postman.
