Fake REST API For Local Testing Using JSON Server
Fake REST API For Local Testing Using JSON Server
JSON Server
https://howtodoinjava.com/angular/mock-rest-server/
Learn to JSON server library for creating a mock REST server in the local development
environment and simulating online REST APIs for unit testing purposes. Using the JSON
server, we can produce desired JSON responses in no time. It is very handy to have such
REST APIs mocking capability for quick development time.
Console Output:
The best thing about this approach is that you are not forced to consume any dummy meaning
less data. Rather you can create JSON data you want to consume and put it in a
file 'db.json' and start serving it.
db.json
{
"employees": [
{ "id": 1, "name": "Lokesh", "status": "active" },
{ "id": 2, "name": "Andy", "status": "Inactive" },
{ "id": 3, "name": "Brian", "status": "active" },
{ "id": 4, "name": "Charles", "status": "Inactive" }
]
}
And start serving the above db.json file with a simple command. Navigate to the directory
where you placed the db.json and run the following command.
Now test the REST API in the browser using the command
'http://localhost:3000/employees'.
JSON server running
Please note that REST endpoints served from JSON server are determined by data nodes in
db.json. It’s so much easy and powerful.
Happy Learning !!