# dyn-mocker

> A dyn-mock server for front-end development

Latest version **1.1.5** (published 2018-05-20) · Mit license · 0 weekly downloads

## Install

```sh
npm install dyn-mocker
pnpm add dyn-mocker
yarn add dyn-mocker
bun add dyn-mocker
```

## 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.1.5 |
| Published | 2018-05-20 |
| First published | 2017-09-03 |
| Weekly downloads | 0 |
| License | Mit |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | HuangZheng |
| Maintainers | hz932, hzsrc |

## Links

- npm: https://www.npmjs.com/package/dyn-mocker
- npm.io page: https://npm.io/package/dyn-mocker

## Dependencies (1)

- [http-proxy](https://npm.io/package/http-proxy.md) ^1.12.0

## Recent versions

- 1.1.5 (latest) — 2018-05-20
- 1.1.3 — 2018-05-11
- 1.1.2 — 2018-05-11
- 1.1.1 — 2018-05-11
- 1.1.0 — 2018-04-28
- 1.0.9 — 2018-04-28
- 1.0.8 — 2018-01-20
- 1.0.7 — 2017-12-15
- 1.0.6 — 2017-12-04
- 1.0.5 — 2017-12-01
- 1.0.3 — 2017-11-30
- 1.0.2 — 2017-10-28
- 1.0.1 — 2017-10-10
- 1.0.0 — 2017-09-03

## README

# dyn-mocker
A dynamic "mock + proxy" server for front-end development.
Please see the [sample project](https://github.com/hzsrc/dyn-mocker-sample) in source code.

# Usage

## 1. install
	npm i dyn-mocker -D
#### or
	yarn add dyn-mocker --dev

## 2. configuration
	Make a mock directory, and copy the mock-config.js into it.
	Make a root directory as the root of mock files.
	--mock
	----root
	----mock-config.js
	--package.json

## 3. configuration
### sample:
	const config = {
		mockEnabled: true,
		mockPath: ['mock/root', 'mock/root-old'], //模拟文件根目录
		proxyTarget: 'http://your-backend-server.com', //后台接口服务地址（代理目标），为空表示不代理
		isHttps: false, //是否https
		port: 8085, //端口
		checkPath: function (urlPath) { //urlPath校验函数，返回true表示需要进行mock处理，为false直接走代理
			return true
		},
		beforeResponse: function (respData, req) { //数据返回前的回调钩子，respData包含status、headers、body属性
			respData.headers['Access-Control-Allow-Origin'] = req.headers['origin'] || req.headers['Origin'];
			respData.headers['Access-Control-Allow-Credentials'] = 'true';
			respData.headers['Access-Control-Max-Age'] = '600';
			respData.headers['Access-Control-Allow-Headers'] = 'Content-Type,Content-Length,Authorization,Access,X-Requested-With,yxt-token';
			//respData.headers["Access-Control-Allow-Methods"] = "PUT,POST,GET,DELETE,PATCH,OPTIONS";
	
			respData.headers['P3P'] = 'CP="CAO PSA OUR"';
		}
	}
	module.exports = config;
###

Edit the mock-config.js options:
### mockEnabled
	[true] to enable it
### mockPath
	the root path[s] of mock files. String or array of string.
### proxyTarget
	If no mock file for a url request, the http pipe will be reversely proxied to this target server. If this is empty, then won't proxy.
### isHttps
	[true] for https
### port
	Port of the mock server
### checkPath
	A function to check a url which needs to mock or not. eg:
	function (urlPath) {
	    return urlPath.match(/\/api\//);
	}
### beforeResponse
	A function to do customized job before responding.respData contains [status、headers、body] properties. eg:
	function (respData, req) {
	    respData.headers["Access-Control-Allow-Origin"] = req.headers["origin"] || req.headers["Origin"];
	    respData.headers["Access-Control-Allow-Credentials"] = "true";
	    respData.headers["Access-Control-Allow-Headers"] = "Content-Type,Content-Length,Authorization,Access,X-Requested-With,yxt_token";
	    //respData.headers["Access-Control-Allow-Methods"] = "PUT,POST,GET,DELETE,PATCH,OPTIONS";
	}

## 4. Add script to package.json
	"scripts": {
		...
	    "mock": "node ./node_modules/dyn-mocker ./mock/mock-config.js"
	}
## 5. Start mock server
	npm run mock

## 6. Create js files for dyn-mocker in the mock/root directory
### Simple js file:
	// dyn-mocker use json eval mode, but not JSON.parse
	module.export = {
		disabled: 0,
		status: 200,
		"headers": {
			"server": "dyn-mocker",
			"set-cookie": "foo=bar; path=/",
			"cache-control": "no-cache"
		},
		"body": {
			"status": 0,
			"obj": {
				"cfg": {
					"o": {
						"buttons": [
							{
								"id": 1,
								"title": "按钮1"
							},
							{
								"id": 2,
								"title": "按钮2"
							}
						]
					}
				}
			},
			"msg": ""
		}
	}
### Js file with functions:
    module.export = {
        disabled: 0,
        body: function (query, post, header, request) {
            //output log in the node console
            console.log('post data: ' + post)

            //use queryString in url
            //use key word 'this', which point to the yaml root object
            if (query.id == '1') return ok(this.case_1);

            //use http postData
            if (post && post.type == 'test') return ok(this.case_2);

            //use http headers
            if (header["content-type"] == "text/txt") return ok("Hello,txt")

            //use request info
            return ok({default: 'no data', url: request.url})

            function ok(d) {
                return {status: 0, data: d}
            }
        },
        case_1: {
            id: 1,
            b: 2
        },
        case_2: {
            a: 3,
            b: "test"
        }
    }


### Another js:
    module.exports = {
        disabled: 0,
        status: 200,
        headers: function (query, post, header, request) {
            var r = {}
            if (post.rememberPwd && this.checkFn(post, header)) {
                process._cookiev = 'a=b' + (+new Date);
                r['set-cookie'] = process._cookiev + '; Max-Age=30000; path=/';
            }
            else {
                process._cookiev = '';
                r['set-cookie'] = process._cookiev + '; Max-Age=0; path=/';
            }
            return r;
        },
        body: function (query, post, header, request) {
            return {
                status: this.checkFn(post, header) ? 0 : 1,
                data: '', returnValue: 'token-xxxxxxx',
                msg: '用户名密码错误'
            }
        },
        checkFn: function (post, header) {
            if (post.password == '123456' || post.password == '') return true;

            if (post.useRemembered && post.password == '_fakepwd') {
                var cookie = header['Cookie'] || header['cookie'];
                return cookie && cookie.indexOf(process._cookiev) > -1
            }
        }
    }

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