Skip links

Microservices with Node JS

Posted by

Microservices with Node JS

When you’re working on a Javascript project, it will grow in size and start with a couple of challenges: maintaining the code, fixing bugs, implementing new features, and more developers will arrive with different ideas.

Normally, the project begins with a monolithic architecture to put everything in a single unit that includes all the components of the application, which includes APIs, services, databases, etc. Monolithic applications are designed to be self-contained and to be the traditional way to build software.

But we have another way to build software: the Microservices architecture.

Microservices Architecture with Node JS

What is Microservice Architecture?

We can define it as an architecture and organization for software development which implies creating small, independent code-piece-like services. Microservice architectures are a contrast to the Monolithic architectures of the past and emerged as a solution to the old approach to web application development.

Frameworks for microservices

Node JS has a lot of frameworks in its environment, but here we will see the most popular microservices to implement.

  • Nest JS is a framework for building efficient, scalable Node.js server-side applications. It uses modern JavaScript, is built with TypeScript (which preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
  • Fastify is a web framework highly focused on providing the best developer experience with the least overhead and powerful plugin architecture and is inspired by Hapi and Express.
  • Seneca: is a toolkit for writing microservices and organizing the business logic of your app. You can break down your app into “stuff that happens”, rather than focusing on data models or managing dependencies.
  • Molecular: is a fast, modern, and powerful microservices framework for Node.js. It helps to build efficient, reliable & scalable services and provides many features for building and managing your microservices.

Product microservice example

We will use the Fatisfy framework for this easy example. Take a look at the following steps to take on your console.

Install fastify-cli with NPM

npm install — global fastify-cli

The next command scaffolds the project:

fastify generate productMicroservice

Now we need Axios library to implement HTTP requests onto our project, and install Axios with NPM:

npm install axios — save

Inside the new project, we need to find and open the next file in your code editor:

productmicroservice\routes\example\index.js

Add a few code lines to implement an Axios HTTP call to one endpoint. This endpoint will return dummy data to respond to our microservice with the route “products”:

“use strict”;

const axios = require(“axios”);

module.exports = async function (fastify, opts) {

fastify.get(“/products”, async function (request, reply) {

const response = await axios.get(“https://hub.dummyapis.com/products");

const { data } = response;

reply.code(200).send(data);

});

};

Now it is the time to run our project:

npm run start

The easy way to test your microservice endpoint is by using the web browser, opening a new tap on your browser, and typing the next URL:

http://localhost:3000/example/products

You should see a big return of information coming from the product microservice endpoint. If you want to create another microservice (e.g., for clients), you should select one framework or the same (Fastify), and that one will return all the client’s information.

Final Thoughts

To conclude, Node JS has popular frameworks to create your own microservices, or if you like, create one from scratch. You could create many microservices and connect between them to share information. All of this data you can display on a webpage, and this will consume microservices endpoints.

Related Content

Adaptive Clinical Trial Designs: Modify trials based on interim results for faster identification of effective drugs.Identify effective drugs faster with data analytics and machine learning algorithms to analyze interim trial results and modify.
Real-World Evidence (RWE) Integration: Supplement trial data with real-world insights for drug effectiveness and safety.Supplement trial data with real-world insights for drug effectiveness and safety.
Biomarker Identification and Validation: Validate biomarkers predicting treatment response for targeted therapies.Utilize bioinformatics and computational biology to validate biomarkers predicting treatment response for targeted therapies.
Collaborative Clinical Research Networks: Establish networks for better patient recruitment and data sharing.Leverage cloud-based platforms and collaborative software to establish networks for better patient recruitment and data sharing.
Master Protocols and Basket Trials: Evaluate multiple drugs in one trial for efficient drug development.Implement electronic data capture systems and digital platforms to efficiently manage and evaluate multiple drugs or drug combinations within a single trial, enabling more streamlined drug development
Remote and Decentralized Trials: Embrace virtual trials for broader patient participation.Embrace telemedicine, virtual monitoring, and digital health tools to conduct remote and decentralized trials, allowing patients to participate from home and reducing the need for frequent in-person visits
Patient-Centric Trials: Design trials with patient needs in mind for better recruitment and retention.Develop patient-centric mobile apps and web portals that provide trial information, virtual support groups, and patient-reported outcome tracking to enhance patient engagement, recruitment, and retention
Regulatory Engagement and Expedited Review Pathways: Engage regulators early for faster approvals.Utilize digital communication tools to engage regulatory agencies early in the drug development process, enabling faster feedback and exploration of expedited review pathways for accelerated approvals
Companion Diagnostics Development: Develop diagnostics for targeted recruitment and personalized treatment.Implement bioinformatics and genomics technologies to develop companion diagnostics that can identify patient subpopulations likely to benefit from the drug, aiding in targeted recruitment and personalized treatment
Data Standardization and Interoperability: Ensure seamless data exchange among research sites.Utilize interoperable electronic health record systems and health data standards to ensure seamless data exchange among different research sites, promoting efficient data aggregation and analysis
Use of AI and Predictive Analytics: Apply AI for drug candidate identification and data analysis.Leverage AI algorithms and predictive analytics to analyze large datasets, identify potential drug candidates, optimize trial designs, and predict treatment outcomes, accelerating the drug development process
R&D Investments: Improve the drug or expand indicationsUtilize computational modelling and simulation techniques to accelerate drug discovery and optimize drug development processes