Complete a rest API service without coding in 30 seconds

Time:2022-6-20

JSON Server

Quickly complete a simulated rest API service without coding in 30 seconds.

This service is mainly used by front-end personnel who need a fast simulation prototype back-end interface

GitHub:https://github.com/typicode/j…

install

$ npm install -g json-server

Example

Create a new db JSON file

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "lucy" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

Start service

$ json-server --watch db.json

In less than 30 seconds, a complete rest API is basically completed.

OK, now open it directly in the browserhttp://localhost:3000/posts/, you can see the data return

{ "id": 1, "title": "json-server", "author": "lucy" }

Points needing attention

  • Support corresponding get, post, put, patch and delete requests
  • The value of ID cannot be modified. The attribute ID will be ignored when using put and patch requests. It will be received only when post is used and the corresponding ID value has not been used
  • The requested body JSON must be a complete JSON string, such as:{"name": "lucy"}
  • Request header settings for post, put and patchContent-Type: application/json

After understanding the above points, you can call them like normal rest APIs

  • All data in the get Posts list

Complete a rest API service without coding in 30 seconds

  • Data with ID 1 in the get Posts list

Complete a rest API service without coding in 30 seconds

  • Post add a piece of data to the posts list

Complete a rest API service without coding in 30 seconds

The ID here does not need to be manually assigned, and the program will automatically increment.

Others can use postman to test requests. Anyway, it’s convenient

Of course, there are other more powerful functions, such as paging, sorting, multi condition query, fuzzy query and so on. You can directly view official documents:https://github.com/typicode/j…

Recommended Today

React series (V) — from mixin to hoc

Series of articles React series (I) — 2013 origin OSCON – react architecture by vjeux React series (II) — Realization of react basic syntax React series (III) — JSX, synthetic events and refs React series (IV) — Analysis on the implementation of virtualdom diff algorithm React series (V) — from mixin to hoc React series […]