# ace-mock

> A fast and easy to use local API mock service

Latest version **2.5.3** (published 2021-07-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install ace-mock
pnpm add ace-mock
yarn add ace-mock
bun add ace-mock
```

Provides the command `ace-mock`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.5.3 |
| Published | 2021-07-08 |
| First published | 2019-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.6.0 |
| Dependencies | 8 |
| Unpacked size | 7.2 MB |
| Known vulnerabilities | 0 (+13 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 8 |
| Author | kybetter |
| Maintainers | kybetter |
| Keywords | ace-mock |

## Links

- npm: https://www.npmjs.com/package/ace-mock
- Repository: https://github.com/kybetter/ace-mock
- Homepage: https://github.com/kybetter/ace-mock#readme
- Issues: https://github.com/kybetter/ace-mock/issues
- npm.io page: https://npm.io/package/ace-mock

## Dependencies (8)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [uuid](https://npm.io/package/uuid.md) ^7.0.3
- [mockjs](https://npm.io/package/mockjs.md) ^1.1.0
- [multer](https://npm.io/package/multer.md) ^1.4.2
- [express](https://npm.io/package/express.md) ^4.17.1
- [pouchdb](https://npm.io/package/pouchdb.md) ^7.2.1
- [socket.io](https://npm.io/package/socket.io.md) ^2.3.0
- [yargs-parser](https://npm.io/package/yargs-parser.md) ^18.1.2

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [instagram-graph-test-server](https://npm.io/package/instagram-graph-test-server.md) — 0 weekly downloads
- [design-recall](https://npm.io/package/design-recall.md) — 0 weekly downloads
- [@msw/playwright](https://npm.io/package/@msw/playwright.md) — 0 weekly downloads

## Recent versions

- 2.5.3 (latest) — 2021-07-08
- 2.5.2 — 2020-10-09
- 2.5.1 — 2020-10-09
- 2.5.0 — 2020-04-04
- 2.4.9 — 2020-04-04
- 2.4.8 — 2020-01-21
- 2.4.7 — 2020-01-21
- 2.4.6 — 2020-01-20
- 2.4.5 — 2020-01-20
- 2.4.4 — 2020-01-20
- 2.4.3 — 2020-01-20
- 2.4.2 — 2020-01-20
- 2.4.1 — 2020-01-20
- 2.4.0 — 2020-01-19
- 2.3.0 — 2020-01-17
- … 10 more at https://npm.io/package/ace-mock/versions

## README

# 一款前端 api mock 工具

#### 界面预览

![api](./ace-mock-preview.png)

#### 安装
```sh
$ yarn global add ace-mock
# 或
$ npm i -g ace-mock
```

#### 使用

```
$ ace-mock [options]
```

#### 示例

```sh
$ ace-mock
# 或
$ ace-mock --port=12345
```

执行上述命令后，就会开启 mock 服务，地址为：`http://localhost:12345`

## 更多功能（持续奇思妙想中，欢迎贡献）

1、ace-mock 支持 Mock.js 语法，如有需要，可以参考：http://mockjs.com/examples.html

**示例：**

```json
{
  "list|30": [
    {
      "id": "@guid",
      "title": "@ctitle(10, 20)",
      "date": "@date('yyyy-MM-dd')"
    }
  ]
}
```

2、支持字段回显，语法：`@request(field)`

**示例：**

请求列表接口的时候，需要用到翻页，因为当前页数是由前端控制的，所以可以使用`@request`语法来回显前端发送过来的页数：

```json
{
  "code": 200,
  "message": "ok",
  "currentPage": "@request(page)",
  "total": 50,
  "resultList|10": [
    {
      "id": "@guid",
      "title": "@ctitle(10, 20)",
      "date": "@date('yyyy-MM-dd')"
    }
  ]
}
```

假设接口为：POST /test

这时前端请求：`curl -X POST -d page=2 http://localhost:12345/test`

返回：

```json
{
  "code": 200,
  "message": "ok",
  "currentPage": 2, <--这里被替换为请求的字段了
  "total": 50,
  "resultList": [
    //...
  ]
}
```

3、支持文件上传，语法：`@upload()`

示例，上传照片：

```json
{
  "status": 200,
  "message": "ok",
  "result": "@upload()"
}
```

前端使用表单上传单张照片后会返回如下结果：

```json
{
  "status": 200,
  "message": "ok",
  "result": "http://localhost:12345/files/file-1579452917264.jpg"
}
```

批量上传多张照片后会返回如下结果：

```json
{
  "status": 200,
  "message": "ok",
  "result": [
    "http://localhost:12345/files/file-1579452927117.jpg",
    "http://localhost:12345/files/file-1579452927118.jpg",
    "http://localhost:12345/files/file-1579452927125.jpg"
  ],
}
```

4、模拟网络延迟，语法：`"@delay": 1000`，单位：ms

示例：

```json
{
  "@delay": 1000, 
  "status": 200,
  "message": "ok",
  "result": "@upload()"
}
```

这样在请求该接口后，服务端将会延迟 1000 毫秒才会向前端响应结果，这样在测试慢速网络下的交互或逻辑异常是非常有帮助的。

5、模拟接口的各种状态码，比如，模拟接口异常：503 状态
```json
{
  "@status": 503,
}
```

6、GET 方法的接口可以使用 HTML

示例：

![html](./html-preview.png)

这时，访问：`http://localhost:12345/page1` 就可以看到所编辑的页面了

你可以使用它来做一些测试，样式调整等，很方便。

甚至，你还可以在服务器上启动 ace-mock，就可以在线编辑网页，做一些简单的页面展示。

---
_Source: https://npm.io/package/ace-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
