aboutsummaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/mock/server.js
blob: 45773284eea0eda81c6ecac5308dd656f75a3bed (plain)
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
88
const jsonServer = require('json-server');
const server = jsonServer.create();
const middlewares = jsonServer.defaults();
// const customersRouters = require('./routes');

// 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('./fake/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(() => {
        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
    // console.log(customersRouters, "===customersRouters")
    // server.use(jsonServer.rewriter(customersRouters))
    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 😁')
});