This repository was archived by the owner on Mar 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
87 lines (79 loc) · 3.42 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* global process, __dirname */
const express = require("express");
const path = require("path");
const axios = require('axios');
const fs = require('fs');
const fsPromises = require('fs').promises;
const htmlParse = require('node-html-parser');
const APP_CONST = require('./config/constants');
let distPath = path.resolve(__dirname, "./dist");
let configPath = path.resolve(__dirname, "./config");
const app = express();
app.use(express.static(distPath));
app.get("/micro-frontends-config", async function (req, res) {
let mfeRoutes
var env_config = APP_CONST.APP_CONFIG.find(function (item) { return item.appEnv == process.env.APPENV.toLowerCase() })
switch (process.env.APPENV.toLowerCase()) {
case APP_CONST.APP_ENV_PROD.toLowerCase() :
case APP_CONST.APP_ENV_DEV.toLowerCase() :
case APP_CONST.APP_ENV_QA.toLowerCase() :
mfeRoutes = await axios.get(env_config.mfeConfigPath)
mfeRoutes = mfeRoutes.data
break;
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
mfeRoutes = await fsPromises.readFile(path.join(configPath + env_config.mfeConfigPath))
break;
default :
return res.send({'error': { message: "Check application environment", code: 500 }})
}
res.send(mfeRoutes);
});
app.get("*", async function (req, res) {
var env_config = APP_CONST.APP_CONFIG.find(function (item) { return item.appEnv == process.env.APPENV.toLowerCase() })
if (!await fs.existsSync(distPath)){
await fs.mkdirSync(distPath);
}
let mfeIndex
switch (process.env.APPENV.toLowerCase()) {
case APP_CONST.APP_ENV_PROD.toLowerCase() :
case APP_CONST.APP_ENV_DEV.toLowerCase() :
case APP_CONST.APP_ENV_QA.toLowerCase() :
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
mfeIndex = await fsPromises.readFile(path.join(distPath + env_config.mfeIndexPath))
break;
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
mfeIndex = await axios.get(env_config.mfeIndexPath)
mfeIndex = mfeIndex.data
break;
default :
return res.send({'error': { message: "Check application environment", code: 500 }})
}
let mfeRoutes
switch (process.env.APPENV.toLowerCase()) {
case APP_CONST.APP_ENV_DEV.toLowerCase() :
case APP_CONST.APP_ENV_QA.toLowerCase() :
case APP_CONST.APP_ENV_PROD.toLowerCase() :
mfeRoutes = await axios.get(env_config.mfeRoutesPath)
mfeRoutes = mfeRoutes.data
break;
case APP_CONST.APP_ENV_LOCAL.toLowerCase() :
case APP_CONST.APP_ENV_LOCAL_MULTI.toLowerCase() :
mfeRoutes = await fsPromises.readFile(path.join(configPath + env_config.mfeRoutesPath))
break;
default :
return res.send({'error': { message: "Check application environment", code: 500 }})
}
let mfeIndexHtml = htmlParse.parse(mfeIndex)
let singleSpaMain = mfeIndexHtml.querySelector('#single-spa-main')
singleSpaMain.remove()
let singleSpaRouter = mfeIndexHtml.querySelector('single-spa-router')
singleSpaRouter.insertAdjacentHTML('beforeend', '<div id="single-spa-main"></div>')
singleSpaMain = mfeIndexHtml.querySelector('#single-spa-main')
singleSpaMain.insertAdjacentHTML('beforeend', mfeRoutes)
await fsPromises.writeFile(path.join(distPath + "/index.html"), mfeIndexHtml.toString());
res.sendFile(path.join(distPath + "/index.html"));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT);
console.log(`App is hosted on port ${PORT}.`); // eslint-disable-line no-console