Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deepraining/js-mocker
使用 js 文件生成接口模拟数据. Use js files to make mock data.
https://github.com/deepraining/js-mocker
Last synced: about 1 month ago
JSON representation
使用 js 文件生成接口模拟数据. Use js files to make mock data.
- Host: GitHub
- URL: https://github.com/deepraining/js-mocker
- Owner: deepraining
- License: mit
- Created: 2020-09-04T11:13:51.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-25T09:07:15.000Z (over 4 years ago)
- Last Synced: 2025-01-10T10:22:42.833Z (about 1 month ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.en.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# js-mocker
[中文文档](./README.md)
Use js files to make mock data.
## quick start
Install js-mocker (Only works in local, not in global):
```
npm install js-mocker --save-dev
```Usage (You can use [npm scripts](https://docs.npmjs.com/misc/scripts) or [npx](https://www.npmjs.com/package/npx) to run this command):
```
js-mocker [options]
```## options
- `dir`: mapping all files under `dir` directory to api, based on file's path
- `-p, --port `: Port to use (defaults to 8092)## used libraries
- [commander.js](https://github.com/tj/commander.js)
- [browser-sync](https://github.com/BrowserSync/browser-sync)## How to use js files to generate mock data
`url`: `/api/user/profile?id=1`
First try `/api/user/profile.js`:
```
// export a function
export default (req, res) => {
// do everything you want
};// or export an object, a string
export default {
success: true,
message: 'ok',
data: { ... },
};
```Second try `/api/user.js`:
```
// export a function
export const profile = (req, res) => {
// do everything you want
};// or export an object, a string
export const profile = {
success: true,
message: 'ok',
data: { ... },
}
````req, res` refers to [Node Http](https://nodejs.org/dist/latest-v8.x/docs/api/http.html), and file name should not contain `.` character, or it will be treated as a static file.
### Support dynamic url
If you need dynamic url, like `/article/{{articleId}}/comment/{{commentId}}`(`/article/1234/comment/5678`), you can use `$d` to replace dynamic ones.
For example, you can make `/article/$d/comment/$d.js` to proxy all urls.
Only starts with number(0-9), will be treated as dynamic url. So, `/article/abc` and `/article/a123` are not dynamic urls.