- Node Package Manager(NPM) provides a module name JSON-SERVER for creating Fake JSON API for rapid application development.
- In this demo, “We will learn to create a fake JSON API using JSON-SERVER module”.
- To install JSON-SERVER use npm install json-server -g command in a terminal.The following screenshot shows the terminal with json-server installation in progress.
- After successful installation of JSON-SERVER module we can find this package in node_modules directory.The following screenshot shows the node_modules directory.
- To demonstrate json-server usage we have created a json file name data.json.The content of data.json are as follows.
{
"students":[
{"id":123,"name":"Sandeep","subject":"Computer","marks":23},
{"id":193,"name":"Raja","subject":"Mathematics","marks":25},
{"id":223,"name":"Smith","subject":"Geography","marks":20},
{"id":313,"name":"Sandeep","subject":"History","marks":21}
]
}
- Now we can use JSON-SERVER to make REST URI for students from data.json file.To make JSON URI use the json-server data.json command in terminal.The following screenshot shows the terminal with json-server in execution.While running it ask for taking backup of original data.json file as snapshot.
- Now the fake json server API can be accessed using port 3000 in localhost.The following screenshot shows the json-server API built for students.
- Now we can access all student data using URL.The following screenshot shows to access all student as JSON data.
- Now We can access a single student data by URL using the ID field.The following screenshot shows to access single student as JSON data.
- The log message for json-server console are as follows.
- You can find more information using following link.
https://github.com/typicode/json-server
