Json-server Analog Rest API

Source: Internet
Author: User

In general, an app's data needs to wait for the interface personnel to develop the corresponding interface can be obtained, so the efficiency is a bit low. It is best that we can simulate the interface data by ourselves, fill the data of the page, get through all the joints, and then develop the interface, and change the interface address.

The tool we're going to talk about today:Json-server can do that.

1, installation Json-server

First of all our computers are installed with Nodejs, and then use the NPM command to install:sudo npm install-g json-server

mewifi:~ cjy$ npm install-g json-server/usr/local/bin/json-server-/usr/local/lib/node_modules/json-server/bin/ index.js-[email protected] node_modules/json-server/node_modules/compression/node_modules/ms/usr/local/lib└─┬[ Email protected]   ├─┬[email protected]   │├──[email protected]   │└──[email protected]   ├──[email Protect ED]   ├─┬[email protected]   │└─┬[email protected]   │   └──[email protected]   ├──[email protected]    ├─┬[email protected]  │└─┬[email protected]  │   └─┬[email protected]  │     └─┬[email Protected]  │       └─┬[email protected]   │         └──[email protected]   └─┬[email protected]    └─┬[ Email protected]      └─┬[email protected]        └─┬[email protected]           

Verify that the installation is successful, view the Json-server configuration entry:json-server-h

mewifi:~ cjy$ json-server-h/usr/local/bin/json-server [options] <source> options:--config,-c Path to con Fig file [default: "Json-server.json"]--port,-p Set Port [default:]--host                                ,-H Set host [default: ""]--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 onl Y 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: "."] --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 value: ' Id ']--quiet,-Q suppress log messages from output [                                   Boolean]--help,-h Display Help information [Boolean]--version,-V Show version number [Boolean] Example:/usr/local/bin/json-server db.json/usr/local/bin/json-server File.js/usr/local/bin/json  -server Http://example.com/db.jsonhttps://github.com/typicode/json-server

2. Operation

Create a JSON file in any directory, such as creating a Data.json file in the JSON directory, writing the following, and executing json-server in the JSON directory Data.json

{"    list": {        "news": [            {
"NewsId": 1, "title": "Title 1", "content": "Chef Crying" }, {
"NewsId": 2, "title": "Heading 2", "content": "How Could" } ] }, "detail": { "object": { "Name": "I am a sledgehammer", "sex": 1, "Iscomplete": True }} }

If you succeed, you will see something like this

mewifi:~ cjy$ CD/work/json/mewifi:json cjy$ json-server data.json  \{^_^}/  hi! Loading Data.json  done  Resources  http://localhost:3000/list  http://localhost:3000/detail  Home  http://localhost:3000  Type s + Enter at any time to create a snapshot of the database

Next, you will be able to access:

3. Common operation

Json-server supports GET, POST, PUT, PATCH, and DELETE methods, and provides a range of query methods, such as Limit,order.

Here is the main introduction of its powerful filtering function .

Sample Data Source:

[    {        "newsId": 1,        "title": "Heading 1",        "content": "Chef Crying"    },    {        "newsId": 2,        "title": "Title 2",        "content": "How Can"    }]
Property value (Filter)

Using . Action object property values

Get content with a length of 4 data

Sorting (sort)

Use _sort and _order (default use Ascending (ASC))

Sort in descending order of NewSID

Split (Slice)

Use _start and _end or _limit

Get 1 data starting from subscript 0 (first object)

Full Text Search

Use q to traverse the object in all value to find the data that contains the specified value

Find all fields that contain "possible" data

Use _likePerform fuzzy lookups (regular expressions are supported)

Find data that contains the words "crying" in content

There are some operations, you can refer to this article: Json-server description

Json-server Analog Rest API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.