How to create a REST API using json-server npm package ?
Last Updated :
06 Aug, 2024
This article describes how to use the json-server package as a fully working REST API.
What is json-server?
json-server is an npm(Node Package Manager) module/package, used for creating a REST API effortlessly. Data is communicated in JSON(JavaScript Object Notation) format between client and server.
Installation: Execute the below command in your project directory.
npm install json-server
Creating a database file: The data is stored in JSON format. Create a .json file that stores the data in JSON format. This JSON file works as a database. for example, we name the database file db.json. json-server recognizes the id attribute specially and treats it with unique constrain. This attribute is useful in getting data accessed individually by id.
Note: double quotes must be used for string value-pairs, and value pairsThe last key-value pair should not be followed by a comma.
Let data be:
{
"students": [
{
"id": 1,
"name": "alex",
"grade": 11,
"marks": {
"maths": 80,
"physics": 50,
"chemistry": 35
}
},
{
"id": 2,
"name": "gary",
"grade": 12,
"marks": {
"maths": 70,
"physics": 50,
"chemistry": 65
}
},
{
"id": 3,
"name": "stuart",
"grade": 11,
"marks": {
"maths": 80,
"physics": 20,
"chemistry": 90
}
}
]
}
Running the json-server: json-server uses port 3000 as default and the server can be run using the command 3000
json-server db.json
Note: db.json is the name of the JSON file.
Running server on an alternative port: The command to run the server on the alternative port number is
json-server db.json --port PORT_NUMBER
Running a live serve: To get the server updated/re-run on manual changes in the .json file use the command
json-server --watch db.json
On running, the json-server module generates a home route for the data server which can be accessed through the URL "http://localhost:3000" (if the port is 3000)
Reading data from the database:
We use the GET method to request data from the server. Higher order attribute in our json includes student object which contains records of the student. As json-server automatically generates routes for higher order attributes.
Example:
1. Retrieving all students
The URL to request Student data from the database is http://localhost:3000/students (the server is running at port 3000).
Below GIF shows get to request which is done in thunder client (An extension in vs code). Tools like postman and web browsers can also be used.
2. Retrieving student by id
Note: id in json-server data is treated as a special attribute. The server allows data only with the unique id.
The URL to retrieve students by their id is http://localhost:3000/students/1 This URL retrieves the student data whose id is "1". This URL pattern doesn't work for other attributes like name, grade, etc.(http://localhost:3000/students/name doesn't work). The below GIF shows the working of this request.
Adding data to the database:
We use the post method to send data requests to the server. HTTP post method sends a request to the server with data in the request body.
Example: URL to add data to the database is http://localhost:3000/students with post method. let's data to be added is
{
"id": 4,
"name": "binny",
"grade": 12,
"marks": {
"maths": 90,
"physics": 100,
"chemistry": 20
}
}
The below GIF shows the execution of the post method and updated data in the database:
Updating data in the database:
We use the HTTP PATCH method to update data in the database. This method sends a request to the server to update the data where the data to be updated is in the request body.
Example: The URL to update data in the database is http://localhost:3000/students/4 with the patch method. This method updates the data of the student with id "4" with data in the request body. Let's data to be updated is
{
"name": "bunny"
}
The name of the student will be changed to bunny on successful request. The below GIF shows the working of the patch method.
Deleting data in the database:
We use the HTTP DELETE method to delete data in the database. This method sends a request to the server to delete the data. The data to be deleted is specified in the request URL.
Example: The URL to delete the student with a particular id is http://localhost:3000/students/4 with the delete method. This method deletes the data of the student with id "4".The below GIF shows the working of delete method.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read