Ad Code

✨🎆 JOIN MERN, JAVA, PYTHON, AI, DEVOPS, SALESFORCE Courses 🎆✨

Get 100% Placement Oriented Program CLICK to new more info click

Create Local JSON Server to Create Rest API

 1. Install Node.js

Ensure you have Node.js installed on your machine. You can check this by typing node -v in your command terminal. [1]
2. Initialize your project
Open your terminal, create a new directory for your project, and navigate into it:
bash
mkdir my-mock-api
cd my-mock-api
npm init -y
3. Create the JSON file
Create a file named db.json in your project root. This file acts as your database, where every top-level key automatically generates its own API endpoint. Add some sample object arrays into it: [1, 2]
json
{
  "users": [
    { "id": "1", "name": "Alice", "email": "alice@example.com" },
    { "id": "2", "name": "Bob", "email": "bob@example.com" }
  ],
  "posts": [
    { "id": "1", "title": "First Post", "userId": "1" }
  ]
}
Use code with caution.
4. Start the JSON Server
You can launch the server instantly using npx (which downloads and runs the tool on the fly): [1]
bash
npx json-server db.json
Use code with caution.If you prefer to run it globally or locally inside your development environment, you can install it using npm: [1]
bash
# Optional: Install globally
npm install -g json-server

# Run the server with watching enabled
json-server --watch db.json

Post a Comment

0 Comments