How to create a local API server & REST API for testing
-with GET, POST, PUT, PATCH & DELETE examples
To create a mock API you can use below options,
- Mockoon
- json server
Here in this article I am working with json server.
In order to install json server you need to run below command and it is a node package. Just in case if you don’t have node installed, you just need to go to node.js download and there you find options for windows, macOS and the source code. Based on your OS you can download & installed node js. After a successfully installation you can run below command on your command line which will installed json server.
npm install -g json-server
Once json server is installed, you can run below command to start json server (local server).
json-server — watch db.json
db.json is the json file which contains API data.
Your server will be up at http://localhost:3000. The data available on http://localhost:3000 is from db.json file.
To check the db.json file you can go to the same location where you started json server, in my case C:\Users\Gihan.
If you open db.json file from an editor, you can see some data and they are json. It contains posts,comments and profile where you can see them in http://localhost:3000.
http://localhost:3000/posts
http://localhost:3000/comments
http://localhost:3000/profile
Let’s create our own data for testing.
After saving the db.json file, refresh http://localhost:3000, then you can see users resource.
Let’s create some more users and subjects resources,
When you hit http://localhost:3000/subjects it will provide all subject.
When you hit http://localhost:3000/subjects/1 it will provide subject which id is 1.
Let’s see some examples related to GET, POST, PUT, PATCH & DELETE methods
Git repository link :
Let’s meet with another exciting article.
Till Then Happy Testing!!!!