Language: EN

simular-api-rest-conjson-server

Quickly Create REST APIs with JSON Server

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 RESTful server with full functionalities like CRUD (Create, Read, Update, Delete) without the need to set up a complete backend.

Some of the main features of JSON Server include:

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

For more information and to explore all the functionalities of JSON Server, we can visit the official GitHub repository.

How to Install JSON Server

Install JSON Server

We 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. We create a file named db.json at 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 necessary:

npx json-server db.json --port 4000

Making Requests to the API

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

Customizing Routes and Filters

JSON Server allows you to customize routes and add 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’s documentation.