1.0.1 • Published 7 years ago

@huangzj/koa-mock v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
7 years ago

koa-mock

install

npm i --save @huangzj/koa-mock

e.g.

var Koa = require('koa');

const mockMiddleware = require("@huangzj/koa-mock");

var app = new Koa();

app.use(mockMiddleware())

app.listen(8000)

.mock.js

module.exports = {
    // Support type as Object and Array
    'GET /api/users': {
        users: [1, 2]
    },

    // Method like GET or POST can be omitted
    '/api/users/1': {
        id: 1
    },
    'GET /api/uses/45': {
        code: 0,
        data: [],
        msg: "323"
    },
    'GET /api/:id': (ctx, next) => {
        ctx.body = {
            id: ctx.params.id
        }

    },

    'POST /api/users': (ctx) => {
        const id = parseInt(ctx.query.id);
        switch (id) {
            case 1:
                ctx.body = {
                    success: true,
                    data: {
                        id: id
                    },
                };
                break;
            default:
                ctx.body = {
                    success: true,
                    data: {
                        id: id
                    },
                };
                break;
        }
    },

    // Support for custom functions, the API is the same as express@4
    'POST /api/users/create': (ctx) => {
        ctx.body = "OK";
    },
    // /cnode/api 会被代理到 https://cnodejs.org/api, 不能代理 https
    'GET /cnode/(.*)': 'http://shrek.imdevsh.com'
};