summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/mock/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'usecaseui-portal/src/app/mock/server.js')
-rw-r--r--usecaseui-portal/src/app/mock/server.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/usecaseui-portal/src/app/mock/server.js b/usecaseui-portal/src/app/mock/server.js
new file mode 100644
index 00000000..45d6b2b5
--- /dev/null
+++ b/usecaseui-portal/src/app/mock/server.js
@@ -0,0 +1,87 @@
+const jsonServer = require('json-server');
+const server = jsonServer.create();
+const middlewares = jsonServer.defaults();
+
+// Set default middlewares (logger, static, cors and no-cache)
+server.use(middlewares);
+
+// Get mock data
+const fs = require('fs');
+const path = require('path');
+
+let localJsonDb = {}; //import mock datas
+const fakeoriginalData = require('./mock.js'); //import datas created in fakedata.js
+const mockFolder = './src/app/mock/json'; //mock json path folder
+const filePath = path.resolve(mockFolder);
+
+fileDisplay(filePath);
+
+function fileDisplay(filePath) {
+ let fileList = [];
+ let originPath = [];
+ let rewriter = {};
+ // Return filelist on based of filePath
+ const files = fs.readdirSync(filePath);
+ files.forEach((filename) => {
+ // Get filename's absolute path
+ let filedir = path.join(filePath, filename);
+ // Get the file information according to the file path and return an fs.Stats object
+ fs.stat(filedir, (err, stats) => {
+ if (err) {
+ console.warn('Get files failed......');
+ } else {
+ let isFile = stats.isFile(); // files
+ let isDir = stats.isDirectory(); //files folder
+ if (isFile) {
+ fileList.push(path.basename(filedir, '.json'));
+ fileList.forEach(item => {
+ let paser = item.split("_").join("/");
+ originPath.push({ route: `/${paser}`, origin: `/${item}` })
+ originPath.map(route => {
+ rewriter[route.route] = route.origin;
+ })
+ localJsonDb[item] = getjsonContent(item);
+ })
+ }
+ if (isDir) {
+ console.warn("=====> DO NOT support mock data in folder");
+ fileDisplay(filedir);
+ }
+ Object.keys(fakeoriginalData).map(item => {
+ localJsonDb[item] = fakeoriginalData[item];
+ })
+ }
+ })
+ })
+ setTimeout(() => {
+ // console.log(rewriter, "===rewriter", localJsonDb, "===localJsonDb", fileList, "===fileList");
+ // console.log(localJsonDb, "===localJsonDb");
+ serverRewrite(rewriter);
+ runServer(localJsonDb);
+ }, 100)
+}
+function getjsonContent(path) {
+ let newpath = `./src/app/mock/json/${path}.json`;
+ let result = JSON.parse(fs.readFileSync(newpath));
+ return result;
+}
+
+//only multi router data needs jsonServer.rewriter
+function serverRewrite(routerpath) {
+ let routerpathArr = routerpath;
+ //rewrite mock multiple routers here
+ Object.keys(fakeoriginalData).map(item => {
+ let newPath = item.split("_").join("/")
+ routerpathArr[`/${newPath}`] = `/${item}`;
+ })
+ //start to rewrite routers
+ server.use(jsonServer.rewriter(routerpathArr));
+}
+
+function runServer(db) {
+ server.use(jsonServer.router(db));
+}
+
+server.listen(3004, () => {
+ console.log('Mock Server is successfully running on port 3004 😁')
+});