How to create a mock REST api for testing in nodejs

There is a frequent requirement during api development to create a mock api quickly so that other team members can use the same to test their respective part of the project. For example front-end developers might need the api endpoints to test their designs. Rather than wait for the complete api to be finished, developers can create a mock api quickly for testing. This nodejs module by typicode helps front-end developers who need a quick back-end for prototyping and mocking REST apis!

Installation

As usual install the nodejs module using npm.

$ npm install -g json-server

Create a db.json file that mocks a api endpoint. A sample is shown below. The code is in JSON format and include some basic information for 2 users.

{
  "users": [
    {
      "id": 1,
      "first_name": "john",
      "last_name": "doe"
    },
    {
      "id": 2,
      "first_name": "jane",
      "last_name": "roe"
    }
  ]
}

Now start the JSON server.

$ json-server --watch db.json

Now browse to the following url.

http://localhost:3000/users

You should see the following output in your browser.

[
  {
    "id": 1,
    "first_name": "john",
    "last_name": "doe"
  },
  {
    "id": 2,
    "first_name": "jane",
    "last_name": "roe"
  }
]

With the following url.

http://localhost:3000/users/2

You will get the following JSON reply.

{
  "id": 2,
  "first_name": "jane",
  "last_name": "roe"
}

You can also test using cURL although I prefer the Chrome Postman app.

$ curl -X GET http://localhost:3000/users/2

You can also integrate the ‘json-server’ module into nodejs scripts.

var jsonServer = require('json-server');
 
var server = jsonServer.create();
var router = jsonServer.router('db.json');
server.use(router);
server.listen(3000);

Based on the previous db.json file, below are all the default routes.

GET    /users
GET    /users/1
POST   /users
PUT    /users/1
PATCH  /users/1
DELETE /users/1

Note that if you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to db.json. For example if you make a POST request like the following a new user will be added with only the ‘id’ field auto-incremented with the next available id number.

Note that if you manually make any changes to the db.json file you need to restart the json-server program for the new content in the file to take effect.

http://localhost:3000/users

The content in the db.json file after the request will be the following.

{
  "users": [
    {
      "id": 1,
      "first_name": "john",
      "last_name": "doe"
    },
    {
      "id": 2,
      "first_name": "jane",
      "last_name": "roe"
    },
    {
      "id": 3
    }
  ]
}

Apso, navigating to the home page will display a nice welcome screen.

http://localhost:3000/

Filtering resources

You can filter resources like the following using GET.

http://localhost:3000/users?first_name=jane
http://localhost:3000/users?id=1

To make a full-text search on resources, add q.

http://localhost:3000/users?q=oe

For my project purpose the above was all that was required. The library however offers many more features of which more information can be found at its home page.