Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brenner8023/sxf-dev-mock
一种约定式本地动态模拟数据方案的实现
https://github.com/brenner8023/sxf-dev-mock
Last synced: 6 days ago
JSON representation
一种约定式本地动态模拟数据方案的实现
- Host: GitHub
- URL: https://github.com/brenner8023/sxf-dev-mock
- Owner: brenner8023
- Created: 2019-03-16T06:34:58.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-13T15:30:43.000Z (over 2 years ago)
- Last Synced: 2024-10-29T23:35:42.298Z (about 2 months ago)
- Language: TypeScript
- Homepage:
- Size: 3.61 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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");
});
```