DELETE Request to Delete a User

Example: In this example the endpoint deletes the user with the specified id from the server. After deletion, the user will no longer exist in the database.

JavaScript
//delete_request.js

const axios = require('axios');

axios.delete('http://localhost:3000/users/3/')
    .then(resp => {
        console.log(resp.data)
    }).catch(error => {
        console.log(error);
    });

Run the below command to test the PUT Request:

node delete_request.js

Output:

{
  id: '3',
  first_name: 'Shreya',
  last_name: 'def',
  email: 'shreya@gmail.com'
}


How to Create A REST API With JSON Server ?

Setting up a RESTful API using JSON Server, a lightweight and easy-to-use tool for quickly prototyping and mocking APIs. JSON Server allows you to create a fully functional REST API with CRUD operations (Create, Read, Update, Delete) using a simple JSON file as a data source.

Table of Content

  • GET Request Returns a List of all Users
  • POST Request to create a New User
  • PUT Request to Update an Existing User
  • DELETE Request to Delete a User

Similar Reads

Approach

First, Create a JSON file that represents your data model. This JSON file will serve as your database.Run JSON Server and point it to your JSON file using the command npx json-server –watch users.json.Create four different JavaScript files to perform the operations like Create, Read, Update, Delete.To Send a GET Request use the command in the terminal node get_request.js that returns a list of all users stored on the server, Send a POST request to create a new user, this will create a new user with the provided data. Similarly, Send a PUT request to update an existing user. This will update the user with the provided data and for DELETE Request, The endpoint deletes the user with the specified id from the server. After deletion, the user will no longer exist in the database....

Steps to create a REST API with JSON Server

Run the below command to Create a package.json file:...

GET Request Returns a List of all Users

Example: In this example the endpoint returns a list of all users stored on the server. Each user object contains properties such as id, name, and email....

POST Request to create a New User

Example: In this example, Send a POST request to create a new user will create a new user with the provided data....

PUT Request to Update an Existing User

Example: In this example, send a PUT request to update an existing user this will update the user with the provided data....

DELETE Request to Delete a User

Example: In this example the endpoint deletes the user with the specified id from the server. After deletion, the user will no longer exist in the database....