In this guide, we will see how to install a JSON server on your Windows computer. So, if you are a front-end developer or just an aspirant who wants to explore APIs with JSON as your data source, this guide is for you.
What is a JSON-server?
JSON stands for JavaScript Object Notation, a lightweight data interchange format. JSON Server is an effortless tool based on Node.js that enables developers to mimic a RESTful API by using a JSON file as the data source. This is particularly useful for front-end developers as they can create mock APIs without having to write intricate server-side code when a backend API is not yet available. So, if you are using a client computer, and you don’t have access to a backend API or a database, JSON-server is your best bet.
Install JSON-server on Windows computer
To install a JSON server on a Windows computer, we will use the Visual Studio Code IDE. To do so, follow the steps mentioned below.
- Download and install Visual Studio Code
- Install JSON Server
- Create a JSON file using Visual Studio Code
- Start your JSON Server
Let us talk about them in detail.
1] Download and install Visual Studio Code
First of all, we need to download the Visual Studio Code from code.visualstudio.com. It is a free IDE from Microsoft that we will be using to create a JSON file hereinafter. Once you go to the link mentioned earlier, make sure to click on the Windows icon as this tutorial is for Windows.
2] Install JSON Server
Now, we need to install the JSON Server on your computer. First, open the File Explorer and navigate to your project. Then, right-click on a space and select Terminal > Command Prompt. Finally, run the following command to install the JSON server.
npm install -g json-server
JSON server is not installed globally on your computer. In case, you need it locally, for just a particular application, the command mentioned below is what you need to use.
npm i json-server
That’s how you can install a JSON server on your computer.
3] Create a JSON file using Visual Studio Code
After installing JSON Server on our computer, we can go ahead and create a JSON file that acts like a database for our project. To do the same, open Visual Studio Code from the Start Menu. After opening VS Code, click on File > Folder, and then navigate to your project’s location (using the cd command).
After opening the folder, click on the New File icon, and enter <filename>.json. We will give you a sample code that you can paste there.
We will develop a sample API that will send requests to an endpoint. This API will respond to HTTP requests, which makes it a great choice for front-end developers who need to develop their projects quickly. The JSON Server will be used to create this API, allowing developers to perform create, read, update, and delete (CRUD) operations and save data in the JSON format. JSON data is structured with key-value pairs and is written in the following format:
{ "name": "Gregg", "emp-ID": 101, "gender": "Male", "team": "Database" }
Here, “name”, “emp-ID”, “gender” and “team” are Properties, and “Gregg”, “101”, “Male”, and “Database” are Values associated with those properties.
Define data in the JSON file. It can be an array of objects or an object with nested objects. Each object should have a unique ID.
4] Start your JSON Server
Since we have created the JSON file, let’s boot up the server. To do the same, open Command Prompt, navigate to your project’s location using the cd command, and run the following command.
json-server --watch db.json
You will be asked to allow Node.JS Javascript Runtime to be accessed by Private and Public networks, click on Allow. The command will run and give an output. You need to URL mentioned below Index and paste it into a browser to open the JSON server.
The JSON server will start, and you will be able to see the values you have entered. The Properties will be displayed on the screen, and when you click on one of them, you will see the associated values.
That’s it!
Read: How to open JSON file in readable format
How do I install JSON server on Windows?
To install the JSON server on your Windows computer, you need to run the command – npm install -g json-server. This will install the server globally, to install it locally, set your working directory to where your project is located and then run npm i json-server.
Read: How to add or change Time Server in Windows
How to run JSON in Windows?
We can run the JSON file from the Command Prompt. So, open File Explorer, navigate to the directory where your JSON file is located, right-click, and select Terminal. Then, run json-server –watch db.json.
Also Read: How to set up an FTP Server on Windows.