Featured resource
pluralsight tech forecast
2025 Tech Forecast

Which technologies will dominate in 2025? And what skills do you need to keep up?

Check it out
Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

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

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 49m
Published
Clock icon Jun 17, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. 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 the orders, you'll need to fulfill them with the products your company sells.

    Therefore, from a business perspective and by using a microservices architecture, you can already identify two key elements to consider: orders and products.

    Instead of having one single monolith that processes orders and products, you can use a microservices architecture approachto create a service (essentially a RESTful API endpoint) that processes orders, and another endpoint that returns information about the company's products.

    Throughout this lab, you'll explore how to build two microservices: one for processing orders and another for retrieving products. 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 the product-service and order-service subfolders.

    Within the product-service subfolder, you'll find a server.js file, where you will write the code for the products microservice.

    Within the order-service subfolder, you'll find a server.js file, where you will write the code for the orders 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 the step02_task01.js file.

    Within the solution/product-service/server.js file, you'll find the final code for the products microservice.

    Within the solution/order-service/server.js file, you'll find the final code for the orders microservice.

    By default, when this lab opens, you'll see two tabs, both called server.js.

    The first server.js file corresponds to the product-service, whereas the second server.js file corresponds to the order-service.

  2. Challenge

    Creating the Product Service

    You will create the product-service in the first server.js file tab.

    info> Alternatively, you can navigate to the src/product-service folder and open the server.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:

  3. 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 of products. Create a route that listens for GET requests at the /products endpoint.

    When a request is received by the product-service at this endpoint, it will respond with the products data.

  4. 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```:
  5. 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 by id.

    There's just one remaining step to to finish creating the product-service and that is to start the server on port 3000. To start the product-service:

  6. Challenge

    Creating the Order Service

    With the product-service created, it's now time to create the order-service.

    Within the solution/order-service folder, open the server.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 the PORT value to 3001.

    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 the POST endpoint for the order-service.

  7. 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 the product-service to fetch product details. Within the order-service, just after the const { data: product } = await axios.get line: The final task you must complete is to start the order-service.

  8. 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 an order, which the order-service does by fetching a specific product that is passed to the order-service as a POST 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.

Eduardo is a technology enthusiast, software architect and customer success advocate. He's designed enterprise .NET solutions that extract, validate and automate critical business processes such as Accounts Payable and Mailroom solutions. He's a well-known specialist in the Enterprise Content Management market segment, specifically focusing on data capture & extraction and document process automation.

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.