The front and back ends of the project are usually parallel development, and in order to reduce the time it takes to wait for the backend interface to develop, we often need to simulate the backend interface locally to test the front-end effect. This practice is called building a front-end mock.
- Start a static service locally, write the required interface as a JSON file, and place the file in the project directory according to the interface name.
- Start a server as a mock server and use a third-party proxy to delegate the interface of the mock server to the static server.
Create a complete Rest API installation in 30 seconds
First you need to install Nodejs on your PC, it is recommended to use the latest version. The JSON server is then installed globally.
npm install json-server -g
Computers that use Linux and MacOS need to add sudo
sudo npm install json-server -g
After the installation is complete, you can use json-server -h
the command to check if the installation is successful and will appear after success
json-server [Options] <source> options:--config,-c Path to config file [default value:"Json-server.json"]--port,-p Set Port [default value:--host,-H Set host [default value:"0.0.0.0"]--watch,-W Watch file (s) [Boolean]--routes,-R Path to Route S file--Static,-s Set static files directory--read-only,--ro allow only GET requests [Boolean]--no-cors,--nc Disable Cross-origin Resource sharing [bool]--no-gzip,--ng Disable gzip content-encoding [BOOL]--snapshots,-s Set snapshots Direc Tory [Default value: '. ']--delay,-d ADD delay to responses (ms)--ID,-i Set database ID Property (E.G. _ID) [ Default value: "id"]--quiet,-Q suppress log messages from output [Boolean]--help,-h Display Help information [Boolean]--version,-v Display version number [Boolean] Example: json- Server DB. JSON json-server file. js json-server http://example.com/db.jsonhttps://github.com/ Typicode/json-server
Run
After the installation is complete, you can create a file in either directory, xxx.json
such as mock/
a folder, create a db.json
file, write the following, and mock/
execute it under a folder json-server db.json -p 3003
.
{ "News ":[ { "ID ":1, "Title ":Cao said the success of yesterday night, "Date ":"2016-08-12", "Likes ":55, "Views ":100086}, {"ID ":2, "Title ":"Dolphin Discovery for the first time in the Yangtze River basin",Date ":"2016-08-12", "Likes ":505, "views ": 9800}]," comments": [{"id": 1, "news_id": 1, "data": [{"id": 1, "content": "support Central Committee decision"}, {"id ": 2," Content ": " transcription of the Constitution is imperative! " } ] } ]}
For convenience, create a package.json
file, write
{ "scripts": { "mock": "json-server db.json --port 3003" }}
Then use the /mock
Execute command to the directory npm run mock
if the success will occur
3003 \{^_^}/ hi! Loading db.json Done Resources http://localhost:3003/news http://localhost:3003/comments Home http://localhost:3003
If it is not successful, check db.json
that the file is in the correct format.
Json-server Construction Use