summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/mock/server.js
diff options
context:
space:
mode:
authorcyuamber <xuranyjy@chinamobile.com>2019-08-22 16:55:57 +0800
committercyuamber <xuranyjy@chinamobile.com>2019-08-22 16:56:09 +0800
commitd0f5347dc16b5aa9fc95eb520fbc9a1c7b672b09 (patch)
treeb3891d8de290d755d7f0f00d35bb77d3b89ad747 /usecaseui-portal/src/app/mock/server.js
parent56923755c761897cc86ca2457667fcc3e6a0e43f (diff)
feat: change the project structure and add mock data function
Change-Id: I381845bff5eb37d1fab3eba8cf1ae7838df523b7 Issue-ID: USECASEUI-307 Signed-off-by: cyuamber <xuranyjy@chinamobile.com>
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 😁')
+});