# back-mock

> Used to simulate the backend API interface

Latest version **1.0.5** (published 2016-03-31) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2016-03-31 |
| First published | 2014-12-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Author | mpr0xy |
| Maintainers | mpr0xy |
| Keywords | mock, api, server |

## Links

- npm: https://www.npmjs.com/package/back-mock
- Repository: https://github.com/mpr0xy/back-mock
- Issues: https://github.com/mpr0xy/back-mock/issues
- npm.io page: https://npm.io/package/back-mock

## Dependencies (3)

- [multer](https://npm.io/package/multer.md) ^0.1.6
- [express](https://npm.io/package/express.md) ^4.10.6
- [body-parser](https://npm.io/package/body-parser.md) ^1.10.0

## 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
- [carveout](https://npm.io/package/carveout.md) — 0 weekly downloads
- [@rwoll/agent-test-server](https://npm.io/package/@rwoll/agent-test-server.md) — 0 weekly downloads
- [peru-mock](https://npm.io/package/peru-mock.md) — 0 weekly downloads

## Recent versions

- 1.0.5 (latest) — 2016-03-31
- 1.0.4 — 2014-12-30
- 1.0.3 — 2014-12-26
- 1.0.2 — 2014-12-25
- 1.0.1 — 2014-12-25
- 1.0.0 — 2014-12-25

## README

#back-mock

这个一个模拟后端api数据交互的模块。

##安装

```
npm install back-mock
```


##使用

新建一个文件(假如命名为mock.js)。在里面写入如下代码。


```
var mockServer = require("back-mock");
var path = require("path");

mockServer('0.0.0.0', 2000, path.resolve('./mocks'));
```

然后在mock.js的同目录新建一个mocks目录，在mocks目录里新建一个文件api.js。api.js里的内容是如下

```
exports.api_test = {
  path: '/api/test',
  POST: function() {
    return {
      xxoo: "this is post"
    };
  },
  GET: function() {
    return {
      xxoo: "this is get"
    }
  }
};

exports.api_comptest = {
  path: '/api/love',
  GET: function(req){
    if (req.query.name == "girl"){
      return "yes"
    }
    else {
      return "no"
    }
  }
}
```

GET和POST方法里的req是[express](expressjs.com)里的req,具体使用可以参考[express](expressjs.com)。

现在就是现在，运行一下mock.js。

```
node mock.js
```

就可以访问

http://127.0.0.1/api/test

查看结果。

http://127.0.0.1/api/love?name=girl

```
{"data":"yes","status":"ok","description":"no error"}
```

mocks目录里可以放很多.js文件，只要格式和api.js文件一样就行，还要保证各个文件的path参数不一样。

##tips
启动mock.js时，推荐使用[supervior](https://www.npmjs.com/package/supervisor)来启动，这样修改文件后不用重启服务，[supervior](https://www.npmjs.com/package/supervisor)会自动重新启动。

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