http-mockjs
v4.6.2
Published
Simple, independent tools for request mock and proxy
Downloads
18
Readme
http-mockjs
The core feature package of http-mockjs
A tool for the local mock interface.
It has the following features:
- Perform the mock mapping relationship through the httpmock field in the package.json , or configuration file .httpmockrc.
- Support mockjs syntax, flexible configuration of dynamic mock return value;
- Identify express-style urls based on path-to-regexp
Usage
Configuration
Take a configuration file .httpmockrc
as an example:
{
"mockFileName":"mymocks",
"responseHeaders": {
"Content-Type": "application/json",
"X-Proxy-By": "http-mockjs"
},
"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"
}
}
}
Return the file content demo as follows, you can use the mockjs style:
{
"code":0,
"message":"success",
"result":{
"content":true,
"name":"brizer123",
"domain":"@domain()",
"otherUrl":"@url()",
"desciption":"@cparagraph(1, 3)",
"date":"@date('yyyy-MM-dd')"
}
}
mockFileName
The root path where the mock file is stored, default is mocks
routes
Mapping management of the corresponding interface
port
Which port to start mock server, default is 8009
working with webpack-dev-server
Config in devServer.before
devServer:{
before:(app)=>{
mocker(app)
}
}
working with express
set app into mocker:
const app = express();
const {mocker} = require('http-mockjs')
mocker(app)
app.listen(8002)
working with command line tools such as http-server
Proxy the request to the proxy server started by httpmock through the proxy http-server.
"scripts": {
"serve": "http-server -p 8008 -P http://localhost:8001/",
"mock": "httpmock 8001"
}