Json Server setup


npm install -g json-server


In the root directory, create a file db.json:

touch db.json

In db.json, initialize your data model:

{       "users":[]    }

Run the server

 json-server -w db.json


In package.json, add a script to start the server:

 "scripts": {       "start:db": "json-server -w db.json"    },

How to use it

Json server follows the REST conventions. Each http method is mapped as the following:

GET -> /users -> Retrieve all users

GET -> /users/:id -> Retrieve user with the giver id

POST -> /users -> Create a new user

PUT -> /users/:id -> Update a user

DELETE -> /users/:id -> Delete a user

Here is a sample of code using axios to send get and post request to the json server:

    import axios from "axios";  axios  .post("http://localhost:3000/users", { name: "wayne", age: "41" })  .then(response => console.log(response))  .catch(err => console.error(err));  axios  .get("http://localhost:3000/users")  .then(response => console.log(response))  .catch(err => console.error(err));