Giter Club home page Giter Club logo

mock's Introduction

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(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'}

/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

mock's People

Contributors

atian25 avatar elrrrrrrr avatar fengmk2 avatar leoner avatar popomore avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.