The following software is required to run the sample:
- Git
- Node.js
- NPM
Clone the repository:
git clone https://github.com/ArtOfMicrofrontends/05-server-discover.git
Go to the repository's directory and run NPM install:
npm install
Now start the application:
npm start
Follow these steps to implement the same from scratch.
- Initialize the repository:
npm init -y
- Make it a Lerna monorepo:
npx lerna init
- Add an application gateway and two microfrontends:
npx lerna create @aom/app --yes
npx lerna create @aom/mife-1 --yes
npx lerna create @aom/mife-2 --yes
- Register the dependencies:
npx lerna add express pug
- Add
http-proxy
to the gateway:
npx lerna add http-proxy --scope @aom/app
- Add Express to each app (
PORT
should be actually different per app):
"use strict";
const express = require("express");
const app = express();
const port = process.env.PORT || 1234;
app.listen(port, () => {
console.log(`Running at ${port}.`);
});
- Add an index route for the app:
app.get("/", (_, res) => {
res.send("index page.");
});
- Integrate the view engine per app.
app.set('view engine', 'pug')
- Add views such as
views/index.pug
:
html
head
title= title
body
h1= message
p In microfrontend 1.
- Call the views from the microfrontends. Here, we can keep the path relative:
res.render('index', { title: "Sample", message: "MF1" });
- To add assets we need to configure static file hosting:
app.use("/mf2", express.static(path.join(__dirname, "..", "public")));
- Run the application:
npx lerna run serve --stream