- Lab
- Core Tech
![Labs Labs](/etc.clientlibs/ps/clientlibs/clientlib-site/resources/images/labs/lab-icon.png)
Guided: Implementing RESTful Communication Between Node.js Microservices
Unlock the power of microservices with this hands-on Guided Lab focused on building RESTful APIs using Node.js. Dive into the essentials of service architecture, learn to design seamless interactions, and master implementing robust, scalable communication strategies with Express. This guided lab is perfect for developers looking to enhance their API expertise and microservices design.
![Labs Labs](/etc.clientlibs/ps/clientlibs/clientlib-site/resources/images/labs/lab-icon.png)
Path Info
Table of Contents
-
Challenge
Introduction
Welcome to the Guided: Implementing RESTful Communication Between Node.js Microservices Code Lab! In this lab, you will learn how to implement RESFTful communication between Node.js microservices. Before you dive in, take a look at some fundamental concepts:
-
Node.js is a cross-platform JavaScript runtime environment that you can use to create servers, microservices, web apps, command line tools and scripts. Node.js is free and open source.
-
A microservice is an architectural style of creating an application that structures the app as a collection of services focused on specific business capabilities, which are decoupled and can be deployed independently from one another.
-
A RESTful API is an interface that more than one application or system uses to exchange information securely over the internet.
So, for example, say that you are developing a web application that processes
orders
for your clients. In this application, in order to process theorders
, you'll need to fulfill them with theproducts
your company sells.Therefore, from a business perspective and by using a microservices architecture, you can already identify two key elements to consider:
orders
andproducts
.Instead of having one single monolith that processes
orders
andproducts
, you can use a microservices architecture approachto create a service (essentially a RESTful API endpoint) that processesorders
, and another endpoint that returns information about the company'sproducts
.Throughout this lab, you'll explore how to build two microservices: one for processing
orders
and another for retrievingproducts
. You'll see one can be used to interact with the other, as RESTful API endpoints.Therefore you'll build two microservices using RESTful communication:
- Product service: Manages product information.
- Order service: Manages orders and communicates with the Product service to retrieve product details.
By following along, you'll understand the basics of microservices architecture, RESTful principles, and inter-service communication. Some familiarity with JavaScript and REST is beneficial, although not required.
You will write the code for this project under the
src
folder, where you'll find theproduct-service
andorder-service
subfolders.Within the
product-service
subfolder, you'll find aserver.js
file, where you will write the code for theproducts
microservice.Within the
order-service
subfolder, you'll find aserver.js
file, where you will write the code for theorders
microservice.To create the RESTful API that will allow the communication between both microservices, you'll use Express.js. Express.js is a small framework for building RESTful APIs with Node.js.
info> The solution for each step in this lab are available for your reference in the
solution
directory.
Each step might have one or more tasks. For example, the solution for Step 2 - Task 1 can be found in thestep02_task01.js
file.
Within thesolution/product-service/server.js
file, you'll find the final code for theproducts
microservice.
Within thesolution/order-service/server.js
file, you'll find the final code for theorders
microservice.
By default, when this lab opens, you'll see two tabs, both calledserver.js
.
The firstserver.js
file corresponds to theproduct-service
, whereas the secondserver.js
file corresponds to theorder-service
. -
-
Challenge
Creating the Product Service
You will create the
product-service
in the firstserver.js
file tab.info> Alternatively, you can navigate to the
src/product-service
folder and open theserver.js
file.In this step, you will create the foundation for the
product-service
, which is a fundamental part of the whole solution.Write the necessary code to initialize the
product-service
by completing the following: -
Challenge
Fetching All Products
Now that you have initialized the
product-service
, it's time to create a list of products and retrieve it.Your next objective is to fetch all the
products
from a product list. You have created a list ofproducts
. Create a route that listens forGET
requests at the/products
endpoint.When a request is received by the
product-service
at this endpoint, it will respond with theproducts
data. -
Challenge
Fetch a Single Product by the Id
Now that you know how to retrieve all products, it's time to fetch a single product by
id
.You created the following endpoint that is used for fetching a single product by
id
.app.get('/products/:id', (req, res) => { }); ``` Now that you have managed to retrieve a product by a specific ```id```:
-
Challenge
Running the Product Service
Well done for getting here!
By now, you have managed to write code for the
product-service
that fetch all products, and fetch a single product byid
.There's just one remaining step to to finish creating the
product-service
and that is to start the server on port3000
. To start theproduct-service
: -
Challenge
Creating the Order Service
With the
product-service
created, it's now time to create theorder-service
.Within the
solution/order-service
folder, open theserver.js
file.The
server.js
file contains the following code:// The order-service - leave this line as is const express = require('express'); const axios = require('axios'); const app = express(); const PORT = 3001; app.use(express.json());
This code imports the Express.js framework, and also the Axios library, which is used for making HTTP(S) calls.
This code also create an
express
instance, and set thePORT
value to3001
.By invoking
app.use(express.json())
, all responses are returned as JSON.So, to make the
order-service
work, you must create an/order
POST
endpoint which will be used for invoking the product service to fetch product details. To create thePOST
endpoint for theorder-service
. -
Challenge
Call the Product Service
Now that you have created the
order-service
, you must add additional code to the/order
endpoint that can invoke theproduct-service
to fetch product details. Within theorder-service
, just after theconst { data: product } = await axios.get
line: The final task you must complete is to start theorder-service
. -
Challenge
Testing with CURL
Well done for reaching this point of the lab!
In this final step, you are going to run and test both microservices. You'll notice that there are three Terminal tabs open.
1. Go to the first Terminal tab and run the
product-service
by executing the following command:node src/product-service/server.js
2. Using the second Terminal tab, run the
order-service
by executing the following command:node src/order-service/server.js
At this stage, both microservices will be running.
3. Using the third Terminal tab, fetch all the products from the
product-service
by executing the following command:curl http://localhost:3000/products
This will return the list of products.
4. To highlight how both microservices can work together, create an
order
by executing the following command in the third Terminal tab:curl -X POST http://localhost:3001/order -H "Content-Type: application/json" -d '{"productId": 1, "quantity": 4}'
Once you execute this command, you'll see the following result:
{"product":"Laptop","price":1000,"quantity":4,"total":4000}
By doing this, you have seen how both services can communicate with each other.
In this example, you invoked the
order-service
to create anorder
, which theorder-service
does by fetching a specific product that is passed to theorder-service
as aPOST
payload:-H "Content-Type: application/json" -d '{"productId": 1, "quantity": 2}'
Now you have gained the basic knowledge of creating and working with Node.js microservices using Express.js for RESTful communication.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.