Create A REST API With JSON Server
In the following will learn how to setup JSON Server and publish a sample REST API
Most of the time we will not get the flexible dummy REST API end points to practice or learn to validate the CRUD operations, we have an option to create full fake REST API with zero coding
Installing JSON Server
JSON Server is available as a NPM package, The installation can be done by using the Node.js package manager, since we are going to use NPM packages we required Node.js to be installed before moving further
- Install Node.js - download the Node.js and install as per your system requirements (Download Node.js here) and confirm the Node.js is installed successfully on your system as per following the steps shown below
- Install JSON Server - We will install JSON Server now using the NPM package, run the below in command prompt
npm install json-server (or) npm install -g json-server
** by adding -g option we make sure the package is installed globally on our system
- JSON File - Now create a JSON file with some name like data.json and save in some file directory, this is a file data which will be exposed as REST API. a sample file given below for reference
json-server --watch data.json (or) json-server data.json
Now the following HTTP endpoints are created automatically by JSON Server and now we can perform CURD operations
- GET - http://localhost:3000/users
- GET - http://localhost:3000/users/{{id}}
- POST - http://localhost:3000/users
- PUT - http://localhost:3000/users/{{id}}
- PATCH - http://localhost:3000/users/{{id}}
- DELETE - http://localhost:3000/users/{{id}}
- POST REQUEST - To create a new user we need to perform a POST request along with the request body and the new user record will be added to our data.json
- GET REQUEST - Now you can check the newly added user is added to the our JSON Server or not by going with the GET endpoint - http://localhost:3000/users/{{id}}
And now you can perform all other HTTP request with above mentioned endpoints like PUT, PATCH, DELETE
- **Important - Do not close the server which we started from terminal until you preformed all actions, if you close the server the endpoints will not work further, and you can start the server again by following the same steps
- Video Reference -
Thanks
Saravanan Seenivasan
QA Lead Engineer
YouTube
LinkedIn
GitHub
Stack overflow
Postman
Good one Saravanan
ReplyDeleteThanks for reading
DeleteGreat content. :) go high saravnnan
ReplyDeleteThanks Vasanth
DeleteIt's very useful content
ReplyDeleteThanks Priya
DeleteHere all content so useful and helpful for beginner and experience both.This site is so amazing, This sites gives good knowledge of rest-api,This is very helpful for me.
ReplyDeleteThanks Amit
DeleteWaoo Awesome content posting,Many developers would agree that selenium-with-core-java is difficult and complex
ReplyDeletewhereas selenium-with-core-java is simple and so more productive,This site is so amazing.This sites gives good knowledge of selenium-with-core-java.
Thanks Nishant
Delete