2.0.24 • Published 2 years ago

mock-proxy-middleware v2.0.24

Weekly downloads
3
License
MIT
Repository
github
Last release
2 years ago

mock-proxy-middleware

前后端分离项目中的本地mock及远程代理

install

npm install mock-proxy-middleware --save-dev
var mockMiddleware = require('mock-proxy-middleware')

define config /xxx/config.js

module.exports = [
  {
    rules: ['^/api/', ^/common-api/], // array,typeof string or regexp
    rules: '^/api/', // string or regexp
    proxyConfig: {
      host: '12.12.12.12',
      port: 8080,
      isHttps: false, // default the same with original
      timeout: 30000, // ms, default 30000ms
      headers: { // set custom headers to proxy server, default proxy original headers
        cookie: 'xxxx'
      },
      redirect: (path) => { // could config redirect path for remote api
        return path
      },
      excludes: [ // when use proxy mode, this apis use local mode
        '^/api/get_index_data/', // string
        /^\/api\/user_info/, // regexp
        (request, proxyConfig) => { // function
          return request.headers.xxx === '/xxxx/' // any logic
        }
      ],
      fillMissingMock: false, // fill missing mock file when lost
      beforeRequest: (params, options) => {
        return [
          {
            _token: 'xxxxx', // add some extra params or reset key:value in params
          },
          {
            agent: false, // set some extra options for nodejs http(s).request`s options
            auth: 'xxxx'
          }
        ]
      }
    },
    mockConfig: {
      path: 'mock', // project`s mock dir name, default 'mock'
      ext: '.js'
    }
  }
]

if you use express server, you can use it like here:

var app = express()
var config = require('/xxx/config')

app.use(mockMiddleware(config));

app.use(mockMiddleware(
  '/xxx/config.js' // if set the config path as first param,the change is immediate effect when modify config
));

app.use(mockMiddleware(
  config,
  '/xxx/xxx/personal_path_config.js' // optional,prevent modification conflicts, could set the second param as self config, add this config file to .gitignore file
));

app.use(mockMiddleware(
  '/xxx/config.js',
  '/xxx/personal_path_config.js'
));

for example,a api like '/common-api/get_user_info', you can define a js file at ${project}/mock/common-api/get_user_info.js, it`s content like

function (params) {
    return {
        err_no: 0,
        err_msg: '',
        sleep: 1000, // mock 1 second delay
        data: {
            name: 'zhangsan'
        }
    }
}

or

{
    err_no: 0,
    err_msg: '',
    data: {
        name: 'zhangsan'
    }
}

if you want to cache mock status by context, you can do it like this:

let times = 0
return function (params) { // this 'return' is required
  return {
    code: xxx,
    data: {
      times: times++ // this can cache prev value
    }
  }
}

for example another, a api like '/api/a/b/c', you can define a js file at ${project}/mock/api/a_b_c.js if you use gulp-connect server, you can use it like here:

var connect = require('gulp-connect');
var config = require('/xxx/config');
connect.server({
    host: host,
    port: port,
    root: ['/'],
    middleware: function(connect, opt) {
        return [
            mockMiddleware(config || '/xxx/config')  // if set a path of config, config is immediate effect
        ];
    }
});

if you use webpack-dev-server, you can use it like here on webpack.config.js:

var config = require('/xxx/config');
devServer: {
  contentBase: '/dist',
  port: 8888,
  historyApiFallback: true,
  inline: true,
  before: function(app) {
    app.use(mockProxyMiddleware(config || '/xxx/config')) // if set a path of config, config is immediate effect
  }
}

if you look at all of apis at this project, input 'https?:{host}/show-apis', need has mock file and meta about api description

scaffold is a demo project with mock proxy tool scaffold

serverany is a local static server with the mock proxy tool serverany

qa mock for test demo

注意:2.0+版本针对参数做了一些格式调整,不兼容低版本,如果需要低版本请找对应版本(1.9.30)npm包

2.0.24

2 years ago

2.0.23

2 years ago

2.0.22

2 years ago

2.0.21

3 years ago

2.0.20

3 years ago

2.0.19

3 years ago

2.0.17

3 years ago

2.0.18

3 years ago

2.0.15

3 years ago

2.0.16

3 years ago

2.0.13

3 years ago

2.0.14

3 years ago

2.0.11

3 years ago

2.0.12

3 years ago

2.0.10

3 years ago

2.0.9

4 years ago

2.0.8

4 years ago

0.0.14-test

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

0.0.13-test

4 years ago

0.0.12-test

4 years ago

0.0.11-test

4 years ago

0.0.10-test

4 years ago

0.0.9-test

4 years ago

0.0.8-test

4 years ago

0.0.7-test

4 years ago

0.0.6-test

4 years ago

0.0.5-test

4 years ago

0.0.4-test

4 years ago

0.0.3-test

4 years ago

2.0.3

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

0.0.1-test

4 years ago

0.0.0-test

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.9.30

5 years ago

1.9.29

5 years ago

1.9.28

5 years ago

1.9.27

5 years ago

1.9.26

5 years ago

1.9.25

5 years ago

1.9.24

5 years ago

1.9.23

5 years ago

1.9.22

5 years ago

1.9.21

5 years ago

1.9.20

5 years ago

1.9.19

5 years ago

1.9.18

5 years ago

1.9.17

5 years ago

1.9.16

5 years ago

1.9.15

6 years ago

1.9.14

6 years ago

1.9.13

6 years ago

1.9.12

6 years ago

1.9.11

6 years ago

1.9.10

6 years ago

1.9.9

6 years ago

1.9.8

6 years ago

1.9.7

7 years ago

1.9.6

7 years ago

1.9.5

7 years ago

1.9.4

7 years ago

1.9.3

7 years ago

1.9.2

7 years ago

1.9.1

7 years ago

1.9.0

7 years ago

1.8.0

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago