Skip to content

一种约定式本地动态模拟数据方案的实现

Notifications You must be signed in to change notification settings

brenner8023/sxf-dev-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sxf-dev-mock

sxf-dev-mock 是一种约定式本地动态模拟数据的实现

读取项目的 mock 目录,会生成配置对应的接口。

比如:

├── mock
    ├── api.ts
    └── users.ts
└── src
    └── pages
        └── index.tsx

/mock 下的 api.tsusers.ts 会被解析为 mock 文件

示例

// mock/index.js
const foo = require("./foo.json");
const bar = require("./bar");
const mockjs = require("mockjs");

module.exports = {
  // 省略method,则默认为get请求
  "/api/users/1": foo,
  "/api/foo/bar": bar(),

  // 支持标准 HTTP
  "GET /api/users": mockjs.mock({
    "list|100": [
      {
        name: "@city",
        "value|1-100": 50,
        "type|0-2": 1,
      },
    ],
  }),
  "DELETE /api/users": { users: [1, 2] },

  // 支持自定义函数,API 参考 express4
  "POST /api/users/create": (req, res) => {
    res.end("OK");
  },

  // 支持参数
  "POST /api/users/:id": (req, res) => {
    res.send({ users: [1, 2] });
  },
};

服务代码使用方法

// server.js
const express = require("express");

const mockServer = require("mock-server");

const app = express();

mockServer(app);

app.get("/", (req, res) => res.send("hello world"));

app.listen(6001, () => {
  console.log("Example app listening on port 6001!");
  console.log("http://127.0.0.1:6001");
});

About

一种约定式本地动态模拟数据方案的实现

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published