Front-end simulates background data and returns JSON Server

前端开发需要调用接口获取数据,但后台开发较慢,严重影响前端调试工作,所以收集资料,找了这个模拟后台接口数据方式。操作很简单,安装即可使用,无需额外多余配置。
首先电脑要安装nodejs,然后打开命令行工具,输入如下指令,全局安装JSON Server:
npm i -g json-server

Then create a folder in any location and create a json file under the folder with any name. As shown below, a data folder is created on the D drive and the data.json file is created in the folder. This file is our simulation. A place to return to.
Please add image description
Create a json data in the data.json file. Each outermost key in the data must correspond to a {}, or []. As the return data format for simulating an interface, it cannot be a direct string ("name":" xx" is not allowed in the outermost layer)
Please add image description
Then open the command line tool in the folder, and then run the following command to start the JSON Server:

json-server --watch data.json -p 3008

As shown in the figure below, the startup is successful
Please add image description
. You can see that the corresponding resources have been generated. Each link corresponds to a key-value in the outer json. You can access the data by copying the link to the browser.
Please add image description
At this point, the simulation is implemented.
For normal use, the folder can be placed in each front-end project to achieve one data file per project. Of course, the files can also be placed outside to achieve unified simulation back-end data management.

Guess you like

Origin blog.csdn.net/yueyie/article/details/127119262