Application Programming Interfaces (APIs) are generally created and executed for input/output applications wherein the data from one application flows 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 application 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
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 console. In case the template application is hosted on the designated IP; the initial set-up process is done (as shown below).
The second step deals in 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 – first is the HTTP method (GET or POST), second is the URL, and third the handler function. A typical syntax of a router is –
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 database or a particular file in the network. The syntax for creating a resource lookup is shown below –
The fourth and the final step is to design a response for the corresponding request. Generally, API responses includes two key information – 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.