Code Monkey home page Code Monkey 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(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

mock's People

Contributors

atian25 avatar dead-horse avatar elrrrrrrr avatar fengmk2 avatar leoner avatar nightink avatar popomore avatar stonechen avatar xuewang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mock's Issues

关于json 请求的判断疑问

if (!view) {
  return this.body = data;
}
if (IS_JSON_RE.test(this.path)) {
  return this.body = data;
}

就是有点疑问, 如果我需要mock的是一个 json, 那么我准备的 mock 数据理论上就不应该有 __view. 那么直接在第一个判断就返回了.

如果我请求的 user.json 是一个页面, 那么就算我准备了一个__view 那么还是会渲染成 json.

那什么情况下, 我请求一个 json 的 mock, 我还会提供一个__view呢?

auto read mock data on ajax request

Need to meet the conditions:

  • ajax request
  • referer should be a mock url

GET /user/profile.json on /home?__scene=default page, should try to read /user/profile.json/default.js mock data file.

  • exists, return the mock data
  • not exists, return normal data

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.