4.5.2 • Published 3 years ago

http-mockjs-proxy v4.5.2

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

http-mockjs

中文文档

A tool for mock local requests or proxy remote requests.

It has the following functions:

  1. Mock the mapping relationship through the httpmock field in the configuration file .httpmockrc or package.json file;

  2. Support mockjs syntax, flexible configuration of dynamic mocker.

  3. Identify express-style URLs based on path-to-regexp.

  4. Based on http-mockjs-ui, manage configuration file by GUI to improve efficiency.

  5. Convenient initialization and GUI editing experience.


Installation

Under the project directory

npm install --save-dev http-mockjs

Or global installation

npm install -g http-mockjs

Initialize the configuration file

The mock process is according to the .httpmockrc file in the current directory, so the newer needs to generate the default configuration file:

http-mockjs init

A configuration file .httpmockrc will be generated in the current folder with the following contents:

{
  "mockFileName": "mocks",
  "responseHeaders": {
    "Content-Type": "application / json"
  }
}

If you want to know how to write the configuration file. Please see at the bottom of the document.

Visual GUI

Manage configuration files and mock content in GUI visualy More details

npm.io

How to launch GUI:

http-mockjs ui

Usage

It has been updated to version 4. If you are using 3.x, the documentation is see here.

The new version requires only one package to implement visual management and initialization functions.

Act as a proxy server directly from the command line

http-mockjs server -p 8008

Reference example

Work with wepback by passing app object

const {mocker} = require ('http-mockjs')

devServer: {
  port: '8002',
  before: (app) => {
    mocker (app)
  }
}

Reference example

Work with express

Just pass in the app object

const app = express();
const { mocker } = require("http-mockjs");

mocker(app);

app.listen(8002);

Work with http-server and other command line tools:

By proxying the http-server, the request is proxyed to the proxy server started by http-mockjs server.

  "scripts": {
    "serve": "http-server -p 8008 -P http: // localhost: 8001 /",
    "mock": "http-mockjs server -p 8001"
  }

Reference example

Configuration file field description

mockFileName

Where to load mock result

responseHeaders

custom response headers

routes

Specific mapping

routes.path

file path

routes.ignore

whether to skip matches.

Sometimes you want to keep the configuration, but you don't want it to take effect. For example, if a request needs to directly call the result of the remote instead of the local mock, you can use it.

Here is an example of a complete configuration file:

{
  "mockFileName": "mocks",
  "responseHeaders": {
    "Content-Type": "application / json"
  },
  "routes": {
    "GET /j/getSomeData.json": {
      "path": "/api/get.json"
    },
    "POST /p/postData.do": {
      "path": "/api/post.json"
    },
    "GET / user /: id": {
      "path": "/api/user.json"
    },
    "GET / users /: id +": {
      "path": "/api/info.json",
      "ignore": true
    },
    "GET / users / user? Id = 123": {
      "path": "/api/user/123.json",
      "ignore": true
    }
  }
}

The file pointed to is created in the folder specified by mockFileName.

The demo of the returned file is as follows, you can use mockjs style

{
  "code": 0,
  "message": "success",
  "result": {
    "content": true,
    "name": "brizer123",
    "domain": "@ domain ()",
    "otherUrl": "@ url ()",
    "desciption": "@ cparagraph (1, 3)",
    "date": "@ date ('yyyy-MM-dd')"
  }
}
4.5.2

3 years ago

4.5.1

4 years ago

4.4.7

4 years ago

4.5.0

4 years ago

4.4.5

4 years ago

4.4.4

4 years ago

4.4.3

4 years ago

4.4.2

4 years ago

4.4.1

4 years ago

4.4.0

4 years ago

4.3.0

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.2-alpha.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

1.0.1-alpha.0

4 years ago

1.0.0

4 years ago