Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dzy321/node-mocker
https://github.com/dzy321/node-mocker
Last synced: about 20 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/dzy321/node-mocker
- Owner: dzy321
- Created: 2016-10-25T01:43:15.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-01T03:13:53.000Z (over 5 years ago)
- Last Synced: 2024-09-21T11:57:52.514Z (about 2 months ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node-Mocker
[![](https://img.shields.io/npm/v/n-mocker.svg?style=flat)](https://www.npmjs.com/package/n-mocker)
mock service and http api with nodejs.
## Features
- js config file,modify and effective
- mock services
- mock http api power by koa2## Requirements
- Node v6.0+
## config
```javascript
module.exports = (log) => ({ //log inner mock server
services: { // mock service
key: 4, // return number
say(word) {
log('sayaaa');
return `hello1 ${word}!`;
},
promiseTest(n, k) { // return Promise
return new Promise((resolve) => {
setTimeout(() => {
resolve(n + k);
}, 10);
});
},
say2(word) {
throw Error('say2 Error!');
},
},
proxyServers: [ // mock http,each will start a http server
{
port: 8081,
routes: {
'[GET]/api/shops/:shopId': { // return a object
a: 'a',
b: 'b'
},
'[GET]/api/items/:itemId': (ctx) => { // or a function
ctx.body = ctx.params.itemId;
},
},
},
{
port: 8082,
routes: {
'[GET]/api/test': {
c: 'c',
d: 'd',
e: 'e'
},
'[GET]/api/site': (ctx) => {
ctx.body = `siteId111:${ctx.query.id}`;
}
},
},
]
});
```## Use
```console
npm i n-mocker
``````javascript
const mocker = new Mock(path.resolve(process.cwd(), 'config-file.js'));
await mocker.start();
```## Workflow
- `npm install`
- `npm run test`