How to work with JSON Server to get a full fake REST API
- In your current project run
npm i -D json-server
- Create
db.json
at the root folder of the project
- Write in the
db.json
file. Attention: this is a json
file.
{
"users": [
{ "id": "23", "firstName": "Bill", "age": 20 },
{ "id": "40", "firstName": "Alex", "age": 42 }
]
}
- Add in
package.json
of the project
"scripts": {
"json-server": "json-server --watch db.json --port 5000",
}
- This will run in a different process
- Run in a separate terminal:
npm run json-server
- Read messages an the terminal
- Run in the browser:
http://localhost:3000/users
- Run in the browser:
http://localhost:3000/users/23
- See project 222 for example and GraphQL Stephen Grider course, lecture 13 - A Realistic Data Source
- See more at https://www.npmjs.com/package/json-server
- For React: