Json Server in JavaScript
JSON Server in JavaScript
What is JSON Server?
JSON Server is a popular tool that lets you create a full fake REST API with zero coding.
It uses a JSON file as a database and serves the data through REST endpoints.
Great for prototyping, mocking backend APIs, or frontend development without a real backend.
How to Set Up JSON Server
1. Install JSON Server (Node.js required)
npm install -g json-server2. Create a db.json file with sample data
{ "users": [ { "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" } ], "posts": [ { "id": 1, "title": "Hello World", "author": "Alice" } ]}3. Start JSON Server
json-server --watch db.jsonThe server runs by default at
http://localhost:3000.It creates RESTful routes for your data automatically, for example:
GET
/usersPOST
/postsPUT
/users/1DELETE
/posts/1
Using JSON Server with JavaScript (Fetch API Example)
fetch('http://localhost:3000/users') .then(response => response.json()) .then(users => { console.log(users); }) .catch(error => console.error('Error:', error));Features of JSON Server
Supports CRUD operations (Create, Read, Update, Delete).
Supports query parameters, filtering, pagination, and sorting.
Can simulate delays and errors.
Supports custom routes and middleware.
Why Use JSON Server?
Rapid frontend prototyping without backend.
Easy to mock APIs during development.
Useful for testing or demos.
If you want, I can help you set up a JSON Server project step-by-step or show how to extend it!