In this post, we will see how to create a full fake Rest API for testing, using a Javascript library called Json-Server.
Here, it is possible to find the project.
HOW TO INSTALL
First of all, we have to install node.js in our system, because Json Server is available as a NPM package.
Then, using the command npm install -g json-server we will install the library:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-22.33.54-1024x284.png)
Now, we have to create a file json where we will insert our ‘fake data’:
[DATA.JSON]
{
"users": [
{
"id": 1,
"username": "username1",
"password": "password1",
"email": "user1@test.com"
},
{
"id": 2,
"username": "username2",
"password": "password2",
"email": "user2@test.com"
},
{
"id": 3,
"username": "username3",
"password": "password3",
"email": "user3@test.com"
}
]
}
In this case, we will have a list of users with 4 properties.
Finally, with the command json-server –watch data.json, we will start our ‘fake’ server:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-22.44.20.png)
Json Server is running and now, using Postman, we will check everything works fine.
GET ALL
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-22.53.15-1011x1024.png)
GET/Id
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-22.53.28.png)
POST
We insert this object in the body and then, we send the request:
{
"id": 4,
"username": "username4",
"password": "password4",
"email": "user4@test.com"
}
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-23.27.40-1024x378.png)
Finally, using the method GETALL, we can check if it worked:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-23.30.04-1024x993.png)
PUT
We insert this object in the body then, we send the request:
{
"username": "new username2",
"password": "new password2",
"email": "user2@test.com"
}
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-23.33.25-1024x501.png)
Finally, using the method GETALL, we can check if it worked:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-23.35.38-929x1024.png)
DELETE
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-23.37.39-1024x539.png)
Using the method GETALL, we can check if it worked:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/04/Screenshot-2021-04-13-at-23.37.55-886x1024.png)