In this chapter, we will explore the concept of Micro Frontends and how to implement them using Express.js as a backend server. Micro Frontends is an architectural style that breaks up a frontend application into smaller, manageable pieces, allowing teams to work independently on different parts of an application. This chapter will provide a comprehensive guide from basic concepts to advanced implementation, including practical examples.
Micro Frontends extend the concept of microservices to the frontend. Instead of having a single monolithic frontend application, the application is split into smaller, independent applications or modules that can be developed, deployed, and scaled independently. This allows different teams to work on different parts of the application without interfering with each other.
Before we start building, ensure you have the following tools installed:
Create a new directory for your micro frontend application:
mkdir micro-frontend-example
cd micro-frontend-example
Run the following command to create a new package.json
file:
npm init -y
Install Express.js as our backend framework:
npm install express
Create the following directory structure:
micro-frontend-example/
│
├── backend/
│ ├── index.js
│
├── frontend/
│ ├── app1/
│ │ ├── index.html
│ │ └── main.js
│ ├── app2/
│ │ ├── index.html
│ │ └── main.js
│
└── package.json
In the backend/index.js
file, set up a basic Express server:
// backend/index.js
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Serve static files from the frontend directory
app.use('/app1', express.static(path.join(__dirname, '../frontend/app1')));
app.use('/app2', express.static(path.join(__dirname, '../frontend/app2')));
// Default route
app.get('/', (req, res) => {
res.send('Welcome to the Micro Frontend Application');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
app1
and app2
.Run the server using the following command:
node backend/index.js
Visit http://localhost:3000
in your browser to see the welcome message.
In the frontend/app1/index.html
, create a simple HTML file
Micro Frontend App 1
Hello from Micro Frontend App 1
In the frontend/app1/main.js
, add the following code:
// frontend/app1/main.js
console.log('Micro Frontend App 1 is loaded');
In the frontend/app2/index.html
, create another simple HTML file:
Micro Frontend App 2
Hello from Micro Frontend App 2
In the frontend/app2/main.js
, add the following code:
// frontend/app2/main.js
console.log('Micro Frontend App 2 is loaded');
Now that both the backend and frontend applications are set up, you can access them by navigating to the following URLs:
You should see the respective messages for each micro frontend in your browser.
To facilitate communication between micro frontends, we can use an event bus or a shared state management approach. For simplicity, we will demonstrate a basic event bus.
Create an eventBus.js
file in the root of your frontend
directory:
// frontend/eventBus.js
const eventBus = {
events: {},
on(event, listener) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(listener);
},
emit(event, data) {
if (this.events[event]) {
this.events[event].forEach(listener => listener(data));
}
}
};
export default eventBus;
Update the main.js
files of both applications to use the event bus for communication.
In frontend/app1/main.js
:
// frontend/app1/main.js
import eventBus from '../eventBus.js';
console.log('Micro Frontend App 1 is loaded');
eventBus.on('paymentProcessed', (data) => {
console.log(`App 1 received payment processed event with data: ${data}`);
});
// Emit an event
setTimeout(() => {
eventBus.emit('paymentProcessed', { amount: 100 });
}, 2000);
In frontend/app2/main.js
:
// frontend/app2/main.js
import eventBus from '../eventBus.js';
console.log('Micro Frontend App 2 is loaded');
eventBus.on('paymentProcessed', (data) => {
console.log(`App 2 received payment processed event with data: ${data}`);
});
When you load Micro Frontend App 1, it will emit a paymentProcessed
event after 2 seconds, which will be received by both Micro Frontend App 1 and Micro Frontend App 2, demonstrating intercommunication.
In this chapter, we explored the concept of Micro Frontends and how to implement them using Express.js as a backend server. We learned about the benefits of using Micro Frontends and how to create independent frontend applications that can communicate with each other through an event bus. Happy coding !❤️