Sometimes
it is necessary to work with fake REST API for practice projects. To achieve such
necessity we can implement JSON Server as follows:
npm install --save json-server
the above command is to install json-server package on nodejs project. Then next we can create a json file name db.json and can add json data as follows:
{
"users": [
{"id": "23", "firstName": "Bill", "age": 20},
{"id": "44", "firstName": "Samantha", "age": 30}
]
}
Now we need to add script inside the package.json file with this text "json:server": "json-server --watch db.json" and full scripts as follows:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"json:server": "json-server --watch db.json"
},
To run json server we can execute the command as follows:
npm run json:server
We can now access the db.json file's contents using the server url http://localhost:3000/users.
We can now access the db.json file's contents using the server url http://localhost:3000/users.
To know more: https://github.com/typicode/json-server