This time I will show you how json-server creates back-end data, and what are the precautions for how json-server creates back-end data. The following is a practical case, let's take a look.
During the development process, the front-end and back-end are separated or not separated. The interface is mostly developed with the page later, so it is very necessary to establish a rest APL interface to provide virtual data to the front-end, so here I use json- As a tool, server supports CORS and JSONP cross-domain requests, supports GET, POST, PUT, PATCH and DELETE methods, and also provides a series of query methods, such as limit, order, etc. Next, I wrote my own usage as Document
Installation
First you must have a node environment (you must have a node environment if you use json-server) and then install json- globally server
npm install json-server -g
After the installation is completed, check whether the global installation is successful
G:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project>json-server -h index.js [options]
Create a db.json directory in the project root directory, and then write the information
{ "api": [ { "text": "数据统计", "link": "#", "hot": true }, { "text": "数据检测", "link": "#", "hot": true }, { "text": "流量分析", "link": "#", "hot": true }, { "text": "广告发布", "link": "#", "hot": false } ] }
in package.json Add a line of commands to the scripts inside
"json": "json-server db.json --port 3003"
Execute the command in the project directory
npm run json
You will see such an interface in bash
> vue@1.0.0 json g:\demo\JavaScript\Vue\Vue one\project\my-project\Vue_two\my-project > json-server db.json --port 3003 \{^_^}/ hi! Loading db.json Done Resources http://localhost:3003/api Home http://localhost:3003 Type s + enter at any time to create a snapshot of the database
Congratulations on the successful startup!
Enter the web page for access at this time
You can access it at this time
http://localhost:3003/api
You can see the json string written before
The json-server has been started now
The calling code is like this
this.$http.get('http://localhost:3003/api') .then((data) => { console.log(data.body) }, () => { console.log('这里是用了vue-source,后端没有接口') })
I believe I read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
How to use Angular data binding mechanism
How to operate the calendar range selection plug-in
The above is the detailed content of How to create back-end data with json-server. For more information, please follow other related articles on the PHP Chinese website!