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
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
0 Comments
POST Answer of Questions and ASK to Doubt