koa-mock-restful
koa-mock-restful is a koa middleware that creates mocks for REST APIs. It will be helpful when you try to test your application without the actual REST API server.
Features:
🔥 Built in support for hot Mocker file replacement.
🚀 Quickly and easily configure the API via JSON.
Installation
npm install koa-mock-restful --save-dev
Usage
koa-mock-restful dev support mock, configured in mocker.js
.
const mockMap = 'GET /api/user': username: 'admin' sex: 5 { ctxbody = text: 'this is from mock server' ; } { ctxbody = id: ctxparamsid username: 'kenny' ; } { ctxbody = id: ctxparamsid type: ctxparamstype ; } { const password username = ctxrequestbody; if password === '888888' && username === 'admin' ctxbody = status: 'ok' code: 0 token: 'sdfsdfsdfdsf' data: id: 1 username: 'kenny' sex: 6 ; else ctxbody = status: 'error' code: 403 ; } { ctxbody = status: 'ok' message: '删除成功!' id: ctxparamsid; }moduleexports = mockMap;
apiMocker
koa
Using withconst path = require('path');const koa = require('koa');+ const apiMocker = require('koa-mock-restful'); const app = koa(); + app.use(apiMocker({entry:'./mocker/index.js',debug:true});app.listen(8080);