An egg plugin for api mock, enabled under local development default.
$ npm i egg-api-mock --save
Add egg-api-mock
as plugin
// {app_root}/config/plugin.js
exports.apiMock = {
enable: true,
package: 'egg-api-mock',
};
Configuration
// {app_root}/config/config.default.js
exports.apiMock = {
dir: 'mock', // default mock dir
env: [ 'local' ], // enabled under local development default
glob: '**/*.mock.js', // default glob
};
You can use koa-router like methods in ${dir}/${glob}
, default: mock/**/*.mock.js
Example:
mock/1.mock.js
module.exports = {
"/api/user/:id": (ctx) => {
ctx.body = ctx.params.id;
},
"/api/users": {
success: true
},
};
mock/2.mock.js
module.exports = {
"POST /api/post": (ctx) => {
ctx.body = ctx.request.body;
},
};
Then you can request you app:
/api/user/1 : 1
/api/user/2 : 2
/api/users : {success: true}
/api/post : post data