All Projects → zhangshaolong → mock-proxy-middleware

zhangshaolong / mock-proxy-middleware

Licence: other
前后端分析项目中的本地mock和远程代理及接口文档

Programming Languages

javascript
184084 projects - #8 most used programming language
Smarty
1635 projects

Projects that are alternatives of or similar to mock-proxy-middleware

jmitm
Java版本的mitmproxy,对本地浏览器所有的Http(s)请求和响应进行拦截并「重制」;也可充当轻量级B/S版抓包软件;
Stars: ✭ 19 (-26.92%)
Mutual labels:  mock
mock-oauth2-server
A scriptable/customizable web server for testing HTTP clients using OAuth2/OpenID Connect or applications with a dependency to a running OAuth2 server (i.e. APIs requiring signed JWTs from a known issuer)
Stars: ✭ 83 (+219.23%)
Mutual labels:  mock
vite-vue-admin
🎉🎉使用Vite + Vue3 + TypeScript + Element-plus + Mock开发的后台管理系统🎉🎉
Stars: ✭ 97 (+273.08%)
Mutual labels:  mock
dva-boot
🌱 使用CRA(create-react-app v2) 构建的react dva 2 脚手架 支持动态路由、接口数据模拟、按功能分层、并且包含诸多实用的小组件
Stars: ✭ 79 (+203.85%)
Mutual labels:  mock
local-data-api
Data API for local, you can write unittest for AWS Aurora Serverless's Data API
Stars: ✭ 99 (+280.77%)
Mutual labels:  mock
EasyNetworking
🛰Easy networking with async/await
Stars: ✭ 27 (+3.85%)
Mutual labels:  mock
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (+15.38%)
Mutual labels:  mock
Stubmatic
Mock HTTP calls without coding. Designed specially for testing and testers.
Stars: ✭ 118 (+353.85%)
Mutual labels:  mock
akka-mock-scheduler
A mock Akka scheduler to simplify testing scheduler-dependent code
Stars: ✭ 86 (+230.77%)
Mutual labels:  mock
ineeda
Mocking library for TypeScript and JavaScript using Proxies!
Stars: ✭ 53 (+103.85%)
Mutual labels:  mock
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+438.46%)
Mutual labels:  mock
mock
Utilities to help mock behavior, spy on function calls, stub methods, and fake time for tests.
Stars: ✭ 31 (+19.23%)
Mutual labels:  mock
httptest
A Test Environment for HTTP Requests in R
Stars: ✭ 76 (+192.31%)
Mutual labels:  mock
admin-antd-vue
Vue3.x + Ant Design Admin template (vite/webpack)
Stars: ✭ 111 (+326.92%)
Mutual labels:  mock
InstantMock
Create mocks easily in Swift
Stars: ✭ 88 (+238.46%)
Mutual labels:  mock
umock-c
A pure C mocking library
Stars: ✭ 29 (+11.54%)
Mutual labels:  mock
xrm-mock
📚 A fake implementation of the Xrm object model. Written in TypeScript against @types/xrm definitions.
Stars: ✭ 64 (+146.15%)
Mutual labels:  mock
deckard
DNS test harness
Stars: ✭ 28 (+7.69%)
Mutual labels:  mock
any-mock
A configurable mock server,help you mock APIs.
Stars: ✭ 25 (-3.85%)
Mutual labels:  mock
bloomrpc-mock
Toolset library for working with GRPC mocks
Stars: ✭ 25 (-3.85%)
Mutual labels:  mock

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包

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].