Skip to content

Commit

Permalink
Increase the proxy setting method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 4, 2018
1 parent ba5a8b5 commit 9323ab0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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',
Expand Down
13 changes: 10 additions & 3 deletions example/webpack/mocker/index.js
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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: '删除成功!' });
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils/delay.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 9323ab0

Please sign in to comment.