Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/mock
Simple web page mock middleware
https://github.com/koajs/mock
koa-middleware
Last synced: 2 months ago
JSON representation
Simple web page mock middleware
- Host: GitHub
- URL: https://github.com/koajs/mock
- Owner: koajs
- License: other
- Created: 2014-11-20T10:40:59.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-03-25T05:15:44.000Z (over 5 years ago)
- Last Synced: 2024-04-14T13:08:40.609Z (8 months ago)
- Topics: koa-middleware
- Language: JavaScript
- Size: 56.6 KB
- Stars: 40
- Watchers: 11
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - mock - Simple web page mock middleware (Middleware)
README
koa-mock
=======[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][cov-image]][cov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url][npm-image]: https://img.shields.io/npm/v/koa-mock.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-mock
[travis-image]: https://img.shields.io/travis/koajs/mock.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/mock
[cov-image]: https://codecov.io/github/koajs/mock/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/koajs/mock?branch=master
[david-image]: https://img.shields.io/david/koajs/mock.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/mock
[download-image]: https://img.shields.io/npm/dm/koa-mock.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-mockWeb page mock middleware.
---
## Features
- Simple url and mock file mapping rules.
- Support `*.js`, `*.json` and common datas.
- Auto find all scenes and easy change it
![koa-mock](https://cloud.githubusercontent.com/assets/156269/5139054/be4f779e-7193-11e4-8a8d-87f12c9a1dbc.gif)## URL Mapping Rules
Use `?__scene[={scene}]` to select mock scene, default scene is `default`.
### Rules
```
{url}?__scene={scene} => {datadir}{url}/{scene}.js
```## Installation
```bash
$ npm install koa-mock
```## Quick start
Using [nunjucks] template engine for example:
- **NOTICE** You must implement `ctx.render(ctx, view, data)` generator function first.
- Use `__scene[={scene}]` querystring to enable mock and select one mock scene.### `app.js`
```js
const path = require('path');
const nunjucks = require('nunjucks');
const Koa = require('koa');
const mock = require('koa-mock');const app = new Koa();
app.use(mock({
datadir: path.join(__dirname, 'mocks')
}));nunjucks.configure(path.join(__dirname, 'views'));
app.context.render = async function(ctx, view, data) {
ctx.body = nunjucks.render(view, data);
};app.listen(1984);
```### `/mocks` files
- /mocks
- default.js => `{name: 'fengmk2', __view: 'home.html'}`
- /users
- /1
- default.js => `{name: 'default-user', __view: 'profile.html'}`
- fengmk2.js => `{name: 'fengmk2', __view: 'profile.html'}`### `/views` files
- /views
- home.html => `welcome home, {{name}}
`
- profile.html => `profile, {{name}}
`### Request the mock web page
```bash
$ curl http://localhost:1984/?__sceneStatus: 200
welcome home, fengmk2
$ curl http://localhost:1984/users/1?__scene=default
Status: 200
profile, default-user
$ curl http://localhost:1984/users/1?__scene=fengmk2
Status: 200
profile, fengmk2
$ curl http://localhost:1984/
Status: 404
Not Found
```## License
MIT
[nunjucks]: https://github.com/mozilla/nunjucks