How to create a local API server & REST API for testing

Chaya Thilakumara
Chaya Thilakumara
Published in
3 min readMay 19, 2020

-with GET, POST, PUT, PATCH & DELETE examples

To create a mock API you can use below options,

  1. Mockoon
  2. 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
Command Prompt : Installed & Start json server (local server)
        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.

http://localhost:3000

To check the db.json file you can go to the same location where you started json server, in my case C:\Users\Gihan.

Command Prompt
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.

db.json file
http://localhost:3000
http://localhost:3000/posts
http://localhost:3000/posts
http://localhost:3000/comments
http://localhost:3000/comments
http://localhost:3000/profile
http://localhost:3000/profile

Let’s create our own data for testing.

db.json file

After saving the db.json file, refresh http://localhost:3000, then you can see users resource.

http://localhost:3000

Let’s create some more users and subjects resources,

http://localhost:3000
db.json file

When you hit http://localhost:3000/subjects it will provide all subject.

http://localhost:3000/subjects

When you hit http://localhost:3000/subjects/1 it will provide subject which id is 1.

http://localhost:3000/subjects/1

Let’s see some examples related to GET, POST, PUT, PATCH & DELETE methods

https://github.com/chayathilak/Test_API
http://localhost:3000/users

Git repository link :

Let’s meet with another exciting article.

Till Then Happy Testing!!!!

--

--

Chaya Thilakumara
Chaya Thilakumara

Pursue your passion, and everything else will fall into place.