koa-mock
Web 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
URL Mapping Rules
Use ?__scene[={scene}]
to select mock scene, default scene is default
.
Rules
{url}?__scene={scene} => {datadir}{url}/{scene}.js
Installation
$ npm install koa-mock
Quick start
Using nunjucks template engine for example:
- NOTICE You must implement
ctx.render(view, data)
generator function first. - Use
__scene[={scene}]
querystring to enable mock and select one mock scene.
app.js
var path = ;var nunjucks = ;var koa = ;var mock = ; var app = ;app; nunjucks; appcontext { thisbody = nunjucks;}; app;
/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'}
- default.js =>
- /1
- default.js =>
/views
files
- /views
- home.html =>
<p>welcome home, {{name}}</p>
- profile.html =>
<p>profile, {{name}}</p>
- home.html =>
Request the mock web page
$ curl http://localhost:1984/?__scene Status: 200<p>welcome home, fengmk2</p> $ curl http://localhost:1984/users/1?__scene=default Status: 200<p>profile, default-user</p> $ curl http://localhost:1984/users/1?__scene=fengmk2 Status: 200<p>profile, fengmk2</p> $ curl http://localhost:1984/ Status: 404Not Found
License
MIT