diff --git a/README.md b/README.md index c6a88bfe..6b3c8505 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,16 @@ webpack-api-mocker dev support mock, configured in `mocker/index.js`. ```js const proxy = { + // Priority processing. + // apiMocker(app, path, option) + // This is the option parameter setting for apiMocker + _proxy: { + proxy: { + '/repos/*': 'https://api.github.com/', + '/:owner/:repo/raw/:ref/*': 'http://127.0.0.1:2018' + }, + changeHost: true, + }, 'GET /api/user': { id: 1, username: 'kenny', diff --git a/example/webpack/mocker/index.js b/example/webpack/mocker/index.js index b1cba074..40e02289 100644 --- a/example/webpack/mocker/index.js +++ b/example/webpack/mocker/index.js @@ -5,6 +5,13 @@ const delay = require('../../../utils/delay'); const noProxy = process.env.NO_PROXY === 'true'; const proxy = { + // Priority processing. + _proxy: { + proxy: { + '/repos/*': 'https://api.github.com/', + }, + changeHost: true, + }, 'GET /api/:owner/:repo/raw/:ref/(.*)': (req, res) => { const { owner, repo, ref } = req.params; // http://localhost:8081/api/admin/webpack-mock-api/raw/master/add/ddd.md @@ -19,7 +26,7 @@ const proxy = { }); }, 'GET /api/userinfo/:id': (req, res) => { - console.log('---->', req.params) + console.log('-1--->', req.params) return res.json({ id: 1, username: 'kenny', @@ -85,8 +92,8 @@ const proxy = { // } // }, 'DELETE /api/user/:id': (req, res) => { - console.log('---->', req.body) - console.log('---->', req.params.id) + console.log('--2-->', req.body) + console.log('--3-->', req.params.id) res.send({ status: 'ok', message: '删除成功!' }); } } diff --git a/utils/delay.js b/utils/delay.js index 2eada261..7380f891 100644 --- a/utils/delay.js +++ b/utils/delay.js @@ -3,7 +3,7 @@ module.exports = function (proxy, timer = 0) { const mockApi = {}; Object.keys(proxy).forEach(function (key) { const result = proxy[key]; - if (Object.prototype.toString.call(result) === '[object String]' && /^http/.test(result) || timer === 0) { + if (Object.prototype.toString.call(result) === '[object String]' && /^http/.test(result) || key === '_proxy' || timer === 0) { mockApi[key] = proxy[key]; } else { mockApi[key] = function (req, res) {