2.0.0 • Published 7 years ago
koa-mock v2.0.0
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 = require('path');
var nunjucks = require('nunjucks');
var koa = require('koa');
var mock = require('koa-mock');
var app = koa();
app.use(mock({
datadir: path.join(__dirname, 'mocks')
}));
nunjucks.configure(path.join(__dirname, 'views'));
app.context.render = function* (view, data) {
this.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'}
- 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: 404
Not Found
License
MIT
2.0.0
7 years ago
1.6.2
8 years ago
1.6.1
9 years ago
1.6.0
9 years ago
1.5.1
9 years ago
1.5.0
10 years ago
1.4.1
10 years ago
1.4.0
10 years ago
1.3.0
10 years ago
1.2.1
10 years ago
1.2.0
10 years ago
1.1.4
11 years ago
1.1.3
11 years ago
1.1.2
11 years ago
1.1.1
11 years ago
1.1.0
11 years ago
1.0.6
11 years ago
1.0.5
11 years ago
1.0.4
11 years ago
1.0.3
11 years ago
1.0.2
11 years ago
1.0.1
11 years ago
1.0.0
11 years ago