If you want to create a mock RESTful API to test your front-end code without setting up a full back-end environment then a JSON server will help you a lot. In this tutorial, we will learn commands to not only install the JSON server on Ubuntu 24.04 Linux but even how to use it with an example.
What is a JSON server?
JSON Server is an open-source tool developed to provide a fake REST API back-end for prototyping and mocking using a JSON file but without performing any coding. It allows front-end developers to test their code quickly instead of setting a full back-end environment before actually going for the final deployment of a project. JSON Server provides all the typical RESTful routes, allowing for CRUD (Create, Read, Update, Delete) operations. The best part is it can be installed using Nodejs NPM, and can be used on almost all operating systems.
What do we need to perform this tutorial?
- Ubuntu 24.04 LTS Linux system or any other version of it such as Ubuntu 20.04, 22.04, or 23.04.
- Access to the root user or any other with sudo rights.
- Availability of an active internet connection
Step 1: Access Ubuntu 24.04 Terminal
Open the command terminal on your Ubuntu system and run the system update command along with the installation of cURL.
sudo apt update && sudo apt upgrade && sudo apt install curl
Step 2: Install Node.js and npm
First, we need to install Nodejs on our Ubuntu 24.04 server or desktop because JSON Server is built on Node.js. Well, to get the latest LTS version of Node we need to add its official repository manually, here is the command to do that.
The default version of Nodejs available through the Ubuntu 24.04 system repository is 18.x, if you want to use that then simply type: “sudo apt install nodejs
“
Whereas those who want the latest available LTS version of Nodejs, need to add its repository manually. For example, while doing this article the latest LTS version was 20.x, therefore, to get it, use the given command:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Install Nodejs and NPM:
After adding this repository run the installation command, it will configure the Node.js along with npm (Node Package Manager).
sudo apt install nodejs
Verify the installation:
node -v
npm -v
Step 3: Install JSON Server on Ubuntu 24.04
Now that we have the Node.js along with its package manager NPM on Ubuntu 24.04, the installation of JSON Server can easily be done using NPM, here is the command to follow:
sudo npm install -g json-server
To verify the installation of the JSON Server, run:
json-server --version
Example: How to use JSON Server
JSON Server uses a simple JSON file to serve as a database. Create a sample JSON file named db.json
in your desired directory. For this tutorial, we’ll create a simple database for users and posts.
Create a JSON File
Create a directory for your project:
mkdir ~/json-server
cd ~/json-server
Create the db.json file:
nano db.json
Add the following sample data:
{
"users": [
{ "id": 1, "name": "John Doe" },
{ "id": 2, "name": "Jane Doe" }
],
"posts": [
{ "id": 1, "title": "Hello World", "author": "John Doe" },
{ "id": 2, "title": "JSON Server Rocks", "author": "Jane Doe" }
]
}
Save the file and exit the text editor (for nano, press CTRL + X, then Y, and Enter).
Step 5: Start JSON Server
To start the JSON Server, navigate to the directory containing your db.json file and run:
json-server --watch db.json
This command starts a JSON Server that watches the db.json file for changes. By default, the server runs on port 3000. You should see the output as given in the screenshot:
Access the API
Open a web browser or use a tool like Curl or Postman to access the API endpoints. You can use the URLs provided in the terminal output.
For example, to get the list of users, navigate to:
http://localhost:3000/users
To get the list of posts, navigate to:
http://localhost:3000/posts
Conclusion
With minimal setup, JSON Server is a quite useful tool for mocking REST APIs quickly and easily. Front-end developers can focus on their projects and their testing without waiting for back-end services to be ready.
Other Articles: