Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

探索一下mock的解决方案 #33

Open
otakustay opened this issue Mar 26, 2021 · 1 comment
Open

探索一下mock的解决方案 #33

otakustay opened this issue Mar 26, 2021 · 1 comment
Labels
future Just requires attention if we love the future
Milestone

Comments

@otakustay
Copy link
Member

No description provided.

@otakustay otakustay added this to the 2.0 milestone Jun 8, 2021
@otakustay otakustay modified the milestones: 2.0, 3.0 Aug 19, 2021
@otakustay otakustay added the future Just requires attention if we love the future label Aug 27, 2021
@isee15
Copy link

isee15 commented Nov 10, 2022

使用支持json和js文件的简单mock可以满足我们项目的大部分场景需求,实现不超过100行。可以考虑先简单加一个

        if (fs.existsSync(filePath)) {
            // read file from the mock directory
            const mockData = fs.readFileSync(filePath).toString('utf-8');
            res.end(mockData);
        } else if (fs.existsSync(jsFilePath)) {
            delete require.cache[jsFilePath];
            const result = require(jsFilePath);
            // searchParams to json
            const jsonParams = JSON.parse(body || '{}');
            reqUrl.searchParams.forEach((value, name) => {
                jsonParams[name] = value;
            });
            if (result && typeof result === 'function') {
                res.end(JSON.stringify(result(jsonParams)));
            }
        } else {
            console.log(`file ${filePath} not found`);
            res.end(`{"ret": "0","msg": "file ${filePath} not found"}`);
        }
    });

就是url到文件的路径映射。json直接就是response,js支持模板接口返回数据。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future Just requires attention if we love the future
Projects
None yet
Development

No branches or pull requests

2 participants