Create REST API using JSON Server

| Tag rest 
  1. Install JSON Server:
npm i -D json-server
  1. 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
	}
  ]
}
  1. 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"
  }
}
  1. Run the script
npm run server
  1. Check the api: http://localhost:3001/posts

See https://github.com/typicode/json-server


Prev     Next