Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brenner8023/sxf-dev-mock

一种约定式本地动态模拟数据方案的实现
https://github.com/brenner8023/sxf-dev-mock

Last synced: 6 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# sxf-dev-mock

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

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

比如:

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

`/mock` 下的 `api.ts` 和 `users.ts` 会被解析为 mock 文件

## 示例

```js
// 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] });
},
};
```

## 服务代码使用方法

```js
// 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");
});
```