Application Programming Interfaces (APIs) are generally created and executed for input/output applications wherein the data from one application flow to another application in the form of a JSON string. This section would explain the various steps ad logic used to create APIs using the ‘Express’ framework on Node.js.

The most preferred approach to designing an API is the ‘REST’ approach. The key advantage of REST APIs is that it’s resource-based i.e. it forms an interface between two or more applications wherein the information can flow via URLs formatting as either a JSON string or an XML quote as well. All the data transfer requests and calls are made via HTTP protocols (such as GET, POST, etc.); and the same are manipulated using the CRUD methods (create, read, update and delete). Also, by virtue of following an HTTP approach; REST APIs are stateless in nature i.e. at no point is the state of the request persisted in the application. However, at the same time, the response received from the server can be cached for a certain specific period of time.

Stepwise execution of creating APIs using Express on Node.js

1. Set up the application & framework in the local system

The first and foremost step in creating an API is to set up the application & framework in the local system. This is generally done by cloning a sample project repository to the standalone system. Once the system is set up; the database connection needs to be made, and then the template application is run on the console. In case the template application is hosted on the designated IP; the initial setup process is done (as shown below).

Stepwise execution of creating APIs using Express on Node.js

2. Routing of the request

The second step deals with the routing of the request; this defines the behavior or action to be performed by the server corresponding to a particular request signal. There are broadly three components to a router – the first is the HTTP method (GET or POST), the second is the URL, and the third is the handler function. A typical syntax of a router is –

creating APIs using Express on Node.js

3. Resource lookup

The third step of creating REST API using Express deals in ‘resource lookup’. This step is important in case the server is required to browse a particular network resource on receiving a request signal (this could be a database or a particular file in the network. The syntax for creating a resource lookup is shown below –

Stepwise execution of creating APIs using Express on Node.js

4. Design response for the corresponding request

The fourth and final step is to design response for the corresponding request. Generally, API responses include two key information – the first is the status code (indicating whether the API call is successful or failure), and the second is the data transmitted either in the form of JSON or XML. A status code of 200 indicates that the API call is performed successfully whereas a code of 400 indicates that the request call was not served properly.

How to extract the actual information from the JSON string?

Finally, when the API is executed successfully, and a response string has been obtained; the focal point is then to extract the relevant data from the JSON or XML string. In case the request is executed using GET protocol; the data can simply be extracted from the URL using a handler function.

However, in case the request is handled using POST protocol; an additional parser is required which parses the body of the returned JSON and converts them to string; which is then retrieved using handler functions.