Introduction
This is a tool to create a web server and mock data quickily. It consists of express and http-proxy-middleware.
Usage
Installation
npm install --save-dev quick-mock-server
Configuration
You can create a configuration file named .proxyrc
, .proxyrc.json
or .proxyrc.js
in the project root directory. In this file, it must be or export a json object.
This json object is like this:
The options is equal to http-proxy-middleware and the public is the static folders using express to achieve.
Addition:
To make it easier to map interfaces toJSON
files, I added the$
symbol inpathRewrite
to represent path variables.For example:
{
"options": {
"pathRewrite": {
"^/api/$": "/$.json"
}
},
"public": [
"mock"
]
}
If you request the url /api/user
, it will be changed to request /user.json
. Now, if you have a user.json
file in mock folder, you can get its data.
Initalization
var startServer =
Quick Start
- create a folder named
example
and run the following cmd in this folder
npm initnpm install --save-dev quick-mock-server
- make the project with following structure:
example
|
|---- mock
| |
| |---- test.json
|
|---- public
| |
| |---- index.html
|
|---- .proxyrc
|
|---- index.js
|
|---- package.json
- Files content
// index.js
var startServer =
// .proxyrc
// public/index.html
// mock/test.json
// package.json
- open the terminal, run
npm start
. You can seeserver has been started, listening in port 9000
output in terminal if no error. - open
http://localhost:9000
in browser, and you can see the data ofmock/test.json
in web page.