All Projects → koajs → mock

koajs / mock

Licence: other
Simple web page mock middleware

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to mock

koa-session-minimal
Minimal implementation of session middleware for Koa 2
Stars: ✭ 73 (+82.5%)
Mutual labels:  koa-middleware
koa-subdomain
Simple and lightweight Koa middleware to handle multilevel and wildcard subdomains
Stars: ✭ 23 (-42.5%)
Mutual labels:  koa-middleware
koa-ip-filter
koa middleware to filter request IPs or custom ID with glob patterns, array, string, regexp or matcher function. Support custom 403 Forbidden message and custom ID.
Stars: ✭ 23 (-42.5%)
Mutual labels:  koa-middleware
koa-webpack-server
Koa2 webpack all-in-one environment for universal development
Stars: ✭ 14 (-65%)
Mutual labels:  koa-middleware
Egg
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
Stars: ✭ 17,616 (+43940%)
Mutual labels:  koa-middleware
koa-2-acl
ACL middleware of koa 2
Stars: ✭ 18 (-55%)
Mutual labels:  koa-middleware
koa-vue-view
A Koa view engine which renders Vue components on server.
Stars: ✭ 29 (-27.5%)
Mutual labels:  koa-middleware

koa-mock

NPM version build status Test coverage David deps npm download

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 koa-mock

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(ctx, view, data) generator function first.
  • Use __scene[={scene}] querystring to enable mock and select one mock scene.

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

  • /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 => <p>welcome home, {{name}}</p>
    • profile.html => <p>profile, {{name}}</p>

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].