Micro-Services
🧩

Micro-Services

Tags
Micro-Services
express.js
Node.js
Published
March 30, 2023
Author
Oussama Belhadi
Microservices architecture has been gaining popularity in recent years due to its ability to improve scalability, flexibility, and flexibility in software.
Ā 
notion image
Ā 
One popular microservices architecture is ExpressJS, a fast and compact web framework for Node.js. In this blog post, we’ll talk about what microservices are and how ExpressJS can be used to build them, and we’ll also explore the relationship between microservices and containers.
Ā 

Monolithic VS Microservices Architecture :

What are microservices?

Microservices are a software architecture for dividing a single application into smaller, independent applications that communicate with each other via APIs. Each microservice handles a specific task and can be configured and deployed independently, allowing flexibility and scalability.
Ā 
Here’s a detailed architecture of Microservices :
Ā 
There’s a bunch of famous companies which are Microservices based :
Ā 
notion image
Ā 
Ā 
This approach also allows for greater resilience because any issues in one microservice do not affect the entire application. Building microservices using Express JS example :
Ā 
Ā 
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
Ā 
In this example, we create a simple microservice that listens for HTTP GET requests on the root path and responds with the text "Hello World!".
Ā 
Express JS also supports middleware, which allows for additional functionality to be added to the microservice. Middleware functions are executed sequentially for each incoming request, and they can modify the request or response objects, terminate the request-response cycle, or call the next middleware function. Here's an example of how middleware can be used to log incoming requests:
Ā 
const express = require('express'); const app = express(); const port = 3000; app.use((req, res, next) => { console.log(`${req.method} ${req.path}`); next(); }); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
Ā 
In this example, we use the app.use method to add a middleware function that logs the incoming requests' method and path. This middleware function is executed for every incoming request, and it calls the next() function to pass control to the next middleware function.
Ā 

Microservices and containers

Containers, such as Docker, are often used to deploy microservices due to their portability and consistency. Containers allow microservices to be packaged with their dependencies and run in any environment that supports Docker. This approach also allows for easy scaling since multiple instances of the microservice can be run simultaneously.
When using containers, each microservice is typically packaged into its own container, making it easier to manage and deploy. This also allows for easier scaling since individual containers can be scaled up or down independently. Container orchestration tools, such as Kubernetes, can be used to manage multiple containers and automate scaling and deployment.
Ā 

Conclusion

Microservices are a powerful software architecture style that can improve scalability, flexibility, and resilience in software applications. ExpressJS is a popular framework for building microservices due to its simplicity and flexibility, and it can be used to create lightweight, independent services that communicate with each other through APIs. Containers are often used to deploy microservices due to their portability and consistency, and they allow for easy scaling and management of individual microservices.
Ā 
You can find a detailed example, including JWT authentication as a service and other microservices here on my repo.
Ā