Create an REST API in a minute
This blog is suitable for a front-end developer who wants a REST service which provides JSON as response with all HTTP methods like GET, POST, PUT, DELETE, etc. Generally a web service needed the following things:
- A server side language
- A web server
- A database
This is very tedious process to create a simple web server and also require a lots of coding. But now we will learn a much simpler approach in which can speed up this process and save a lots of time. Even we don’t need to learn any specific language. We can create a REST service using one or two npm commands.
Before start this session, we need to install npm on our machine. You can install node JS which comes with npm in its package. You can install node JS from the following link:
After installation, you can test both node and npm using the following commands:
- node –v
- npm –v
These commands will show the installed version of node and npm on your machine.
Create a REST API using JSON Server:
Let’s get start with a simple application. We can create the REST API using a node package called JSON Server with the following easy 3 steps:
- Installing JSON Server:We need to first install this package globally on our system. The installation can be done by the following command:
- npm install –g json-server
Adding –g option will make sure that the package is installed globally on our system.
- Create a JSON file: We need to create a JSON file which will represent as a database. Let’s create a JSON file with name json. This file contains the JSON data which should be exposed by the REST API. The root node of this JSON structure will treated as REST endpoint. We can make CURD operation on this endpoint. Let’s take a look at the sample JSON file:
This JSON contains only one object called employees with 4 sub data set. Each sub data set contains four fields called id, firstName, lastName and email. From this JSON we can expose only one REST resource as it contains one parent object.
- Running the JSON Server: Our JSON needs a web server in order to expose it’s service. Let’s start the JSON server by executing following command:
- json-server –watch db.json
Make sure that the db.json is on your command prompt path.
By default the server will start on 3000 port. We can also change this using –port parameter while starting the server. The following command will start the HTTP server on 2018 port:
json-server –watch db.json –port 2018
Now we can open the URL http://localhost:3000 in the web browser. We will get the following result:
The following HTTP endpoints are created automatically by JSON server:
Test the REST API endpoints using postman:
Here we are using postman for REST API testing. In postman, we can test all requests like GET, POST, PUT, DELETE, etc.
- Get Request:
Get all the employees
Get a particular employee by ID:
Insert an employee
Again get all the employees to test the above insertion.
Update an employee details
Delete an employee by it’s ID
To learn more about this, refer the following link: