Setting Up Local JSON Server and Testing API with Postman

Setting Up Local JSON Server and Testing API with Postman

NEWSLETTER - February 24, 2024

If you're developing a web application that communicates with a backend server, it's essential to set up a local server environment for testing. In this guide, we'll walk you through the process of setting up a JSON server locally and testing API endpoints using Postman.

Setting Up JSON Server Locally

JSON Server is a lightweight, local server that allows you to quickly prototype and develop APIs using JSON data. Here's how to set it up:

Step 1: Install JSON Server

Ensure you have Node.js installed on your machine. Then, open your terminal and run the following command to install JSON Server globally:

npm install -g json-server

Step 2: Create a Database File

Create a file named db.json in your project directory. This file will serve as your database. Populate it with sample data, like so:

 {

  "users": [

    {

      "name": "laljan",

      "email": "lal@gmail.com",

      "password": "12345"

    },

    {

      "name": "basha",

      "email": "basha@gmail.com",

      "password": "12345"

    },

    {

      "name": "jan",

      "email": "jan@gmail.com",

      "password": "12345"

    }

  ]

}

Step 3: Start JSON Server

In your terminal, navigate to the directory containing db.json and run the following command:

json-server --watch db.json

This command starts the JSON Server and watches the db.json file for changes.

Step 4: Accessing Data

Once the server is running, you can access your data via RESTful API endpoints. For example:

Users endpoint: http://localhost:3000/users

Now that your JSON Server is set up, let's proceed to testing the API endpoints using Postman.

Testing API Endpoints with Postman

Postman is a popular tool for testing APIs. Here's how you can use it to test your local JSON Server:

Step 1: Install Postman

If you haven't already, download and install Postman from [the official website](https://www.postman.com/downloads/).

Step 2: Create a New Request

Open Postman and create a new request by clicking on the "New" button in the top-left corner and selecting "Request."

Step 3: Specify Request Details

- Enter a name for your request, e.g., "Get Users".

- Choose the appropriate HTTP method (GET, POST, PUT, DELETE) for your API endpoint.

- In the request URL field, enter the URL of your local server, e.g., localhost:3000/users.

Step 4: Send the Request

Click on the "Send" button to execute the request. Postman will display the response from your local server.

Step 5: Test Different Endpoints and Scenarios

You can repeat the above steps to test various endpoints and scenarios of your API. Postman allows you to create multiple requests and organize them into collections for easier management.

By following these steps, you can effectively set up a local JSON Server and test API endpoints using Postman. This approach is invaluable for developing and debugging APIs before deploying them to a production environment.