Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

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.

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.