What is JSON Server?
JSON Server was created for front-end developers who need a quick back-end for prototyping and mocking. It allows developers to get a full fake REST API using their own data with no coding required.
Install JSON Server
Open your terminal and run
npm i json-server
You may also install it globally by adding the -g
attribute
npm install -g json-server
Create JSON database
In the root of your application, create db.json
file with some data
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
]
}
Configure JSON server
Open package.json
file and add the following line of code in the scripts
object:
"scripts" : {
...
"mock:server": "json-server --watch db.json"
}
If you have installed json-server globally you may simply run json-server --watch db.json
in your terminal.
Run JSON server
npm run mock:server
Open http://localhost:3000/posts/1
in your browser to get the following data
Top comments (0)