1/13/18

Mocking a REST API With Model First Development

When developing application that integrates with JSON APIs, we usually test the API calls with a development API Server. This is a great approach when such server is available. However, when we are also building the API or have no development backend available, we need to use a mock server API to enable our frontend and integration development to move forward.  In this article, we take a look at building a mock server with the Model First Development approach and json-server.

Model First Development

With Model First Development, we focus on developing the application models that are required for a successful integration. After we build the models, we can then focus on building the APIs for our basic CRUD operations.  This is where the use of json-server can help developers build mock servers by just using the defined models.

What is json-server?

Json-server is a Fake REST API server application built on Node.js that reads JSON models and creates basic CRUD operations on those models thus enabling the rapid creation of REST APIs. This is often used by front-end as well as integration developers to test the API calls with a mock server.
We should also note that json-server support other operations like sorting, filtering, paging, search among a few other features. For more information visit the home page at this location:


Installation

The setup for this application requires NPM as well as Node.js. Once those dependencies are installed, we can type the following command to install json-server.


npm install –g json-server


Building our Models

We are going to build a simple vehicle inventory with the year, make and model property names. In our models, we create two collections, makes and vehicles. We use these collections to illustrate how json-server creates different routes for each collection.


{
    "makes":["Nissan","Subaru","Toyota"],   
    "vehicles":[ 
        {"year":"2018","make":"Nissan","model":"Altima"},
        {"year":"2018","make":"Subaru","model":"Outback"},
        {"year":"2018","make":"Toyota","model":"4Runner"}   
    ]
}


The important thing to notice from our JSON sample is that we have included multiple collections in the same file. This is a requirement from json-server. One session of the json-server can only watch a single JSON file. To overcome this problem, we can start multiple sessions on different ports using a different file and port number.


json-server –watch ozkary-inventory.json –port 3005


Starting the Mock server

To start the mock server, we need to pass the –watch parameter with a target JSON file. In this example, we are running the command from the same directory where our JSON file is located.


json-server –watch ozkary-inventory.json


Once we execute this command, we should have a list of the resources (APIs) that are available from our mock server as well as the endpoint which defaults to port 3000.  This is the endpoint that we want to use for our API calls.




Testing the APIs

To quickly test our APIs, we can use Postman or a similar tool to test our GET, POST operations on our models.  When we use a GET operation, we can see the JSON data that comes back from our API. We can send a GET request with the ID parameter to simulate a search request for a specific record.

When we send a POST request, json-server simulates the creation of a new record and returns the id for that record. This is a good way to test out create, updates and delete operations without having to write lots of fake operations.



Summary


With the help of json-server, we are able to mock an API by first focusing on the application models. We then let the json-server handle the logic of the CRUD operations which are inferred by the model definition. This accelerates the front-end and integration efforts without having to implement a mock server.

Originally published by ozkary.com

0 comments :

Post a Comment

What do you think?