Easily mock REST services with json server and zero coding


Often time, as a developer, you are want to consume REST/JSON based APIs provided by third party applications or other teams in your enterprise. Now, if you see in order to continue with your development, you are actually dependent on those others team or third party applications. But can you really wait for them to complete their development before you start your own? In the agile world of development, it’s a complete no-no. It’s necessary that we proactively unblock ourselves and don’t get highly dependent on the other teams to provide services for us start consuming, developing etc. At the same time, we as consumers don’t want to create database, REST services ourselves. So, how do we unblock ourselves and break this deadlock? The answer is that we mock the the REST services for our purpose. There are several tools in market to help you with the REST service mock. In this tutorial, we will look at one such solution that helps us to easily mock REST services with json server and zero coding. As you complete this tutorial, you will realise there is absolutely zero coding. Actually, the official github page calls it so easy that we can spawn a full fledged fake API in less that 30 seconds.

Technology Stack

We use Node.js here.
We’ll use json-server which is a node module and it help you create fake REST API in a jiffy. Ensure that you have Node.js installed in your system (Windows, Linux, Mac anything). I am not going to cover installing Node.js in this tutorial. You may do a little google to check on how to install nodejs if you already don’t have it installed.
I’ll cover this tutorial in my Ubuntu system. However, if you are using any other OS, no issue; you can follow the same instructions.

So, let’s get started.

Install json-server node module

Json server is a node module and is available as NPM package. Hit the following single line command to install json-server in your system.

If you notice, the highlighted thrid line above, it tells you the version of json-server we just installed. Alternatively, you may run json-server -v command to check the version as well.

You may need to use sudo if you don’t have sufficient privilege to run this command.

Create mock Database

When you have REST service and want to mock CRUD operations, you must need a database. How else, you can demo! Well, these wonderful folks behind json-server have made that easy for us as well. You just mock the database. No need to create one for yourself. How do we do that?

Well, we’ll just create a JSON file called dbmockup.json in any directory of our choice and this file will contain all the data we want to expose to consumers. Here, we would like to expose orders data. So, let’s take a look at our content in dbmockup.json file. While you create the mock data, ensure that data must be an object; not array.

Run json server

Now that you are ready with the mock, let’s run the json server from the same location where you have created the data. If you are in some other directory, ensure to provide the right path to the dbmockup.json file.


Consume the REST services

  • HTTP GET all orders from browser

    • Open a browser and hit the url http://localhost:3000/orders and you will see all the orders data listed in the browser.



  • HTTP GET specific Order from browser

    • Open a browser and hit the url http://localhost:3000/orders/1234 and you will see the specific orders data listed in the browser.



  • HTTP POST (add a new order record)

    • Use curl command to add a new Orders record
    •  Hit the http://localhost:3000/orders/ to check the addition of the new record we just inserted,
  • HTTP PUT (modify an order record)

    • Use curl command to update an existing Orders record with id 1234
    • Hit the http://localhost:3000/orders/1234 to check that the order record 1234 is updated with the new information.
  • HTTP Delete

    • Use curl command to delete an existing Orders record with id 1234
    • Hit the http://localhost:3000/orders/1234 to check that the order record 1234 is no more there.


Well, you have seen how easy it’s to mock REST/JSON services and how quickly you can unblock yourself instead of depending on a third party service provider to actually deliver you the service. Now, go develop 🙂



No Responses

Write a response

This site uses Akismet to reduce spam. Learn how your comment data is processed.