koa-mockjs

0.2.0 • Public • Published

koa-mockjs

mockjs api server middleware for Koa2

Linux Build Coverage Status Dependencies node license MIT

中文文档

Requirements

node6 or node8, koa2

How to use it

Installation

$ yarn add -D koa-mockjs
# or 
$ npm i -D koa-mockjs

Quick start

  1. Create a directory api-server, then create the file app.js, the content is:
const { join } = require('path');
const Koa = require('koa');
const mock = require('koa-mockjs');
 
const app = new Koa();
 
// Use the default path '/' (Not recommended)
// app.use(mock('/', join(__dirname, 'mocks')));
 
// Use a custom path '/api'
app.use(mock('/api', join(__dirname, 'mocks')));
 
// Here you can add any code.
 
app.listen(3000, () => {
  console.log('mock server run at http://localhost:3000/api/');
});
  1. Create a mocks directory under api-server and create data.json as follows:
/**
 * api interface description
 *
 * @url /test-api
 */
{
  "code": 0,
  "result|5": [
    {
      "uid|+1": 1,
      "name": "@name",
      "email": "@email"
    }
  ]
}
  1. Install dependent modules
$ yarn add -D koa koa-mockjs
  1. Start
$ node app.js

You can then access the http://localhost:3000/api to view API documents.


Mock JSON

Examples

.
├── mocks
    ├── home
    ⎪   ├── data.json
    ├── user
    ⎪   ├── data.js
    ⎪   ├── data.json
    ├── game
        ├── data.json

data.json

Mock JSON here is not a real JSON file, and more like a JS file, so you want to use the following format.

Hypothetical file in 'mocks/home/test.json'

/**
 * Interface function description
 *
 * @url /api-access-path
 *
 * Parameter description and other instructions.
 * uid: user ID
 * name: username
 * email: the email
 * etc.
 */
 
{
  "code": 0,
  "result|5": [
    {
      "uid|+1": 1,
      "name": "@name",
      "email": "@email"
    }
  ]
}

Then you can access the http://localhost:3000/api/api-access-path through the browser.

Of course, you can also use the JS file directly.

/**
 * home page links
 *
 * @url /home-links
 *
 * Here you can write a detailed description
 * of the parameters of the information.
 */
 
module.exports = {
  code() { // simulation error code, 1/10 probability of error code 1.
    return Math.random() < 0.1 ? 1 : 0;
  },
  "list|5-10": [
    {"title": "@title", "link": "@url"}
  ]
};

Or export function.

/**
 * user page - user info
 *
 * @url /user?uid=233
 *
 * GET: Request method and parameter
 *   uid This is the requested userID
 *
 * Here you can write a detailed description
 * of the parameters of the information.
 */
 
module.exports = function (req) {
  const { uid } = req.query;
 
  if (!uid) {
    return {
      code: -1,
      msg: 'no uid',
    }
  }
 
  return {
    code: 0,
    data: {
      "uid": +uid,
      "name": "@name",
      "age|20-30": 1,
      "email": "@email",
      "date": "@date",
    },
  };
};

Package Sidebar

Install

npm i koa-mockjs

Weekly Downloads

0

Version

0.2.0

License

MIT

Unpacked Size

19.9 kB

Total Files

6

Last publish

Collaborators

  • mr.lou