Introduction
Usually, during development, we will face an issue where some mock or simulator target sample is required to test the Client application.
In that case, we need to develop a mock server which is usually time-consuming as we need to
- Create endpoints
- Develop functions
- Deploy for testing
We can use Json-Server to avoid mock server development.
How to install Json-Server?
It's simple; use the below command.
Step 1
Install Node.js from here: https://nodejs.org/en/
Step 2
npm install -g json-server
To make sure json-server is installed. Just type the below command
json-server --version
Below is the output
How to use it?
Create one sample JSON file with endpoints and what response it has to send on calling those endpoints, as shown below.
Filename: sample.json
{
"EndpointA": {
"A": "B",
"AA": "BB",
"AAA": "BBB"
},
"EndpointB": {
"ABB": "BC",
"AAB": "CBB",
"ABA": "BCC"
},
"EndpointC": {
"CCC": "BC",
"ACB": "BCB",
"C": "BCB"
},
"HeartBeat": {
"Beat": "Dude I am running"
},
"getMyDetails": {
"Name": "MyName",
"Age": 123,
"Job": "Reading",
"JKL": "ABC"
},
"JsonStandsFor": {
"JSON": "Java Script Object Notification"
},
"XmlStandsFor": {
"XML": "Extensible Markup language"
}
}
After that, run the below command to load this JSON file and run it as an API
json-server sample.json
After running this command, in the terminal, we can see which port the server is running and what endpoints are available.
Put the endpoint in the browser; we will get the configured response
Summary
In this article, we understood that creating a mock server is quick and straightforward. I hope it helps in day-to-day development for testing purposes.
Happy Coding ๐