Home

Awesome

koa-mock

NPM version build status Test coverage David deps npm download

Web page mock middleware.


Features

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:

app.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

/views files

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