Json-server – Installation and basic use

Json-server can quickly generate interfaces from existing JSON files

Using the step

Json-server is installed globally

Previously installed global package: Nodemon, NRM

Nodejs is a third-party package that relies on NodeJS. It is a standalone tool, not limited to a project, and can be installed globally.

npm i json-server -g
Copy the code

Sudo NPM I json-server-g

1. Prepare an empty folder

In any directory, prepare an empty folder and name it mock-server(you can change it to something else)

2. Create a JSON file

Create a new file named db.json in the folder.

In any directory, prepare an empty folder and name it mock-server(you can change it to something else)

3. Initialize the structure

In the db.json file, define an object according to the json format requirements:

  • Key-value pair format
  • Put it in double quotation marks
{
  "assets": [{"id": 1."name": "Coat"."price": 99 },
        { "id": 2."name": "Pants"."price": 34 },
        { "id": 3."name": "Shoes"."price": 25.4 },
        { "id": 4."name": "Hair"."price": 19900}}]Copy the code

4. Enable the interface service

Automatically generate the interface from the db.json created above.

Go to the folder created in the previous step

In this folder, open the command line window and enter the command json-server db.json -p 8888 (json-server has a space after it).

5. Test

Access the above address in your browser

Note:

  • In the generated interface addressassetsIs the same as the property name in db.json.
  • The content of db.json must be in JSON format.
  • Property name –> Address of the interface
  • Attribute values — – > interface data returned (array) | object

RESTful interface

The target

Understand the definition of restFul style

The problem to import

Every programmer has their own way of writing interfaces: give them different names, such as implementation add assets

Classmate A: localhost: 3000 / addAsset | delAsset

Classmate B: localhost: 3000 / insertAsset | deleteAsset

Classmate C: localhost: 3000 / TJZJ | SCZJ

RESTful interface

In view of the above problems, a set of conventions is proposed. RESTful interfaces are standardized by:

  • Request method to determine operation categories (Add, Delete, Modify, query)
  • Use a noun to indicate the object to be acted upon

Json-server provides restful interfaces.

test

Take the preceding service as an example: Once the service is successfully started, the following interface addresses are automatically generated.

Address of the interface Request way Operation type
localhost:3000/assets GET Get all data
localhost:3000/assets/1 GET Get single data
localhost:3000/assets POST Add operations. Parameters: {name, price,}
localhost:3000/assets/1 DELETE Delete operation
localhost:3000/assets/1 PUT Complete modification. Parameters: {name, price,}
localhost:3000/assets/1 PATCH Partial modification. Parameters: {name}

The preceding interfaces are restful interface rules.

Reference documentation