simular-api-rest-conjson-server

Quickly Create REST APIs with JSON Server

  • 3 min

JSON Server is an open-source tool that allows you to create a RESTful API from a JSON file.

It is an incredibly useful tool for developing and testing web applications by allowing us to quickly create a complete REST API from a JSON file.

JSON Server emulates a fully functional RESTful server with complete CRUD (Create, Read, Update, Delete) capabilities without the need to set up a full backend.

Some of the main features of JSON Server are:

  • Quick Setup: Allows us to start an API server with minimal configuration, using only a JSON file.
  • CRUD Support: Provides full support for Create, Read, Update, and Delete operations.
  • Customization: Allows defining custom routes, search filters, and performing other configurations.
  • Ideal for Prototypes: Perfect for testing and rapid development, before implementing a full backend solution.

How to Install JSON Server

Install JSON Server

Run the following command to install JSON Server in our project:

npm install json-server

Create a JSON File

We need a JSON file to serve as our database. Create a file named db.json in the root of our project with the following example content:

{ “posts”: [ { “id”: 1, “title”: “Post 1”, “author”: “Author 1” }, { “id”: 2, “title”: “Post 2”, “author”: “Author 2” } ], “comments”: [ { “id”: 1, “body”: “Comment 1”, “postId”: 1 }, { “id”: 2, “body”: “Comment 2”, “postId”: 1 } ] }

This file defines two resources: posts and comments, each with some initial data.

How to Use JSON Server

With JSON Server installed and our JSON file created, we can start the server and begin interacting with our RESTful API.

Start the Server

To start the server, we use the following command, specifying the JSON file we want to use:

npx json-server db.json

This command starts the server and watches for changes in the db.json file. By default, the server runs on port 3000, but we can specify a different port if needed:

npx json-server db.json —port 4000

Making API Requests

Once the server is running, we can make HTTP requests to the API using tools like Postman, Insomnia, or curl, or directly from our code. Here are some examples of how to interact with the API:

  • Get all posts:

curl http://localhost:3000/posts

  • Get a specific post:

curl http://localhost:3000/posts/1

  • Create a new post:

curl -X POST -H “Content-Type: application/json” -d ’{“title”: “Post 3”, “author”: “Author 3”}’

  • Update an existing post:

curl -X PUT -H “Content-Type: application/json” -d ’{“title”: “Updated Post 1”, “author”: “Author 1”}’ http://localhost:3000/posts/1

  • Delete a post:

curl -X DELETE http://localhost:3000/posts/1

Route and Filter Customization

JSON Server allows customizing routes and adding filters to handle more complex requests. For example, we can use query parameters to filter results:

curl http://localhost:3000/posts?author=Author%201

We can also define custom routes and create custom controllers by creating a server.js file. For more information, read the project documentation.