- Install JSON Server:
npm i -D json-server
- Define the data behind the API in a JSON file db.json:
{
"posts": [
{
"title": "Getting started with fetch",
"description": "How to interact with backend APIs using fetch",
"id": 1
},
{
"title": "Getting started with useEffect",
"description": "How to use React's useEffect hook for interacting with backend APIs",
"id": 2
}
]
}
- Define npm script to start the JSON server.
Open package.json and add a script called server:
{
...
"scripts": {
...
"server": "json-server --watch db.json --port 3001"
}
}
- Run the script
npm run server
- Check the api: http://localhost:3001/posts
See https://github.com/typicode/json-server