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
          The above JSON structure consists employees object with 2 data set 


  • Running The Server - Let's run the JSON server by running below command on the terminal
    json-server --watch data.json  (or)  json-server data.json
 

By default the server will open in localhost:3000 port, now you can open the URL in Browser or Postman and you will get the following results that comes from our JSON file which we created earlier



 Now the following HTTP endpoints are created automatically by JSON Server and now we can perform CURD operations 

  1. GET - http://localhost:3000/users
  2. GET - http://localhost:3000/users/{{id}}
  3. POST - http://localhost:3000/users
  4. PUT - http://localhost:3000/users/{{id}}
  5. PATCH - http://localhost:3000/users/{{id}}
  6. 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


Finally you can check the logs in server terminal, that all actions that we do from postman will be getting recorded 

  • **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








        

Comments

  1. Great content. :) go high saravnnan

    ReplyDelete
  2. Here 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.

    ReplyDelete
  3. Waoo Awesome content posting,Many developers would agree that selenium-with-core-java is difficult and complex
    whereas 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Change The Default Port Number Of JSON-Server

WinAppDriver – Free Tool To Automate Windows Applications