Home > Web Front-end > JS Tutorial > How to create back-end data with json-server

How to create back-end data with json-server

php中世界最好的语言
Release: 2018-06-08 11:10:39
Original
1659 people have browsed it

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
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 the information

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

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

  "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 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,后端没有接口')
   })
Copy after login

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!

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