JSON Server Tutorial
JSON Server is a simple tool for setting up a REST API using a JSON file as a data source. It is great for prototyping, testing, and simulating a backend for frontend applications.
1. Prerequisitesβ
Before getting started with JSON Server, make sure you have the following installed:
- Node.js: You can download it from Node.js Official Website. This will also install
npm(Node Package Manager).
Verify Installationβ
To check if Node.js and npm are installed, run the following commands:
node -v
npm -v
2. Installing JSON Serverβ
You can install JSON Server globally on your system using npm:
npm install -g json-server
Alternatively, you can install it as a dev dependency in your project:
npm install --save-dev json-server
3. Creating a JSON Fileβ
Create a JSON file that will serve as the database. For example, create a file named db.json:
{
"posts": [
{ "id": 1, "title": "Hello World", "author": "John Doe" },
{ "id": 2, "title": "JSON Server is Great", "author": "Jane Doe" }
],
"comments": [
{ "id": 1, "body": "Nice post!", "postId": 1 },
{ "id": 2, "body": "Thanks for sharing!", "postId": 2 }
]
}
4. Running JSON Serverβ
To run JSON Server and serve the db.json file, use the following command in your terminal:
json-server --watch db.json
By default, JSON Server runs on http://localhost:3000. You can specify a different port using the --port option:
json-server --watch db.json --port 4000
Accessing the APIβ
Once the server is running, you can access the resources defined in your JSON file. For example:
- Get all posts:
GET http://localhost:3000/posts - Get a single post:
GET http://localhost:3000/posts/1 - Get all comments:
GET http://localhost:3000/comments - Get comments for a specific post:
GET http://localhost:3000/comments?postId=1
5. Creating Resourcesβ
You can create new resources using HTTP POST requests. For example, to add a new post, you can use curl:
curl -X POST -H "Content-Type: application/json" -d '{"title": "New Post", "author": "John Smith"}' http://localhost:3000/posts
Alternatively, you can use a tool like Postman or Insomnia to make the request.
6. Updating Resourcesβ
To update an existing resource, use the HTTP PUT or PATCH method. Hereβs an example using curl to update a post:
curl -X PUT -H "Content-Type: application/json" -d '{"title": "Updated Post", "author": "John Smith"}' http://localhost:3000/posts/1
7. Deleting Resourcesβ
To delete a resource, use the HTTP DELETE method. Hereβs how to delete a post:
curl -X DELETE http://localhost:3000/posts/1
8. Custom Routesβ
You can create custom routes in JSON Server by using the --routes option with a routes.json file. Hereβs an example routes.json file:
{
"/api/posts": "/posts",
"/api/comments": "/comments"
}
Run JSON Server with custom routes:
json-server --watch db.json --routes routes.json
Accessing Custom Routesβ
With the above routes, you can now access your posts at http://localhost:3000/api/posts.
9. Conclusionβ
JSON Server is a powerful tool for quickly setting up a mock REST API. This tutorial covered the basics of installation, running the server, and performing CRUD operations. Itβs a great way to simulate a backend for your frontend applications.
Further Readingβ
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.