Home > Web Front-end > JS Tutorial > json-server implements back-end data simulation

json-server implements back-end data simulation

php中世界最好的语言
Release: 2018-04-28 13:33:36
Original
1287 people have browsed it

This time I will bring you json-server to implement back-end data simulation. What are the precautions for json-server to implement back-end data simulation. 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 intentions into documents

Installation

First of all, there must be a node environment (all use json-server There must be a node environment) and then install json-server globally

npm install json-server -g
Copy after login

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] <source>
Options:
 --config, -c        Path to config file  [default: "json-server.json"]
 --port, -p         Set port              [default: 3000]
 --host, -H         Set host            [default: "0.0.0.0"]
 --watch, -w        Watch file(s)               [boolean]
 --routes, -r        Path to routes file
 --middlewares, -m     Paths to middleware files          [array]
 --static, -s        Set static files directory
 --read-only, --ro     Allow only GET requests          [boolean]
 --no-cors, --nc      Disable Cross-Origin Resource Sharing   [boolean]
 --no-gzip, --ng      Disable GZIP Content-Encoding       [boolean]
 --snapshots, -S      Set snapshots directory       [default: "."]
 --delay, -d        Add delay to responses (ms)
 --id, -i          Set database id property (e.g. _id) [default: "id"]
 --foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id)
                                 [default: "Id"]
 --quiet, -q        Suppress log messages from output     [boolean]
 --help, -h         Show help                 [boolean]
 --version, -v       Show version number            [boolean]
Examples:
 index.js db.json
 index.js file.js
 index.js http://example.com/db.json
https://github.com/typicode/json-server
Copy after login

Create a db.json directory in the project root directory, and then write it Information

{
 "api": [
  {
   "text": "数据统计",
   "link": "#",
   "hot": true
  },
  {
   "text": "数据检测",
   "link": "#",
   "hot": true
  },
  {
   "text": "流量分析",
   "link": "#",
   "hot": true
  },
  {
   "text": "广告发布",
   "link": "#",
   "hot": false
  }
 ]
}
Copy after login

Add a line of commands to the scripts in package.json

  "json": "json-server db.json --port 3003"
Copy after login

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
Copy after login

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 is now started.

The calling code is as follows After initializing the

this.$http.get('http://localhost:3003/api')
   .then((data) => {
    console.log(data.body)
   }, () => {
    console.log('这里是用了vue-source,后端没有接口')
   })
Copy after login

page, you can see that the data is completed

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps for cropping images with Vue-cropper

Detailed explanation of the steps for using the Vue Mixin function

The above is the detailed content of json-server implements back-end data simulation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template