Building Restful Services using Express – Introduction

1 min read

Rest simply stands for Representational State Transfer. It is the convention for building http services. Using http principle, we can provide support for client applications to Read, Create, Update and Delete data. We refers to this operations as CRUD operations.

For example, consider an online radio streaming service that can serve various client applications with a list of radio stations. Such client applications include Mobile apps on Android, iOS and Windows. In order to manage the list of radio stations on the client app, we will need to expose a service on the server, which we usually refer to as an endpoint of the format below.

http://radioapp.com/api/v1/stations

Explanation

With the above url, the client application can send http requests to the endpoint, in order to communicate with the server. The endpoint address can either be http or https. The use of either http or https solely depends on the kind of application you are building, as well as its requirements. For instance, in situations where sensitive informations are accessed such as credit card information, it is a good practice to enable data exchange over a secure channel like https.

The second part is the domain of the application, which also represents the base url of the web service.

Thirdly, we have the ‘/api/v1‘. This is just a convention and part of standard practices to follow when building Restful APIs. The name api helps in understanding the part to the application endpoints on the server, while the v1, v2 and so on refers to the versions of the web service.  

The last part is the ‘/stations’, which refers to the resource. All operations such as creating a new radio station, updating an existing station, reading the list of radio stations and also deleting radio station information from the database will be performed by sending an http request to the station endpoint.

HTTP Request Types

The type of the http request determines the kind of the operation that will be carried out. In other words, every HTTP request has a verb or method for determining its types. Some of the popular standard http methods includes:

  • GET – for retrieving list of data
  • POST – for creating a new data
  • PUT – for updating an existing data
  • DELETE – for deleting a data

Using the online radio streaming application discussed earlier, let’s explain how each of the http request above can come into play, together with the possible response from the server.

HTTP GET REQUEST – GET LIST OF STATIONS

image showing an example of GET request from client and the response from server

GET REQUEST – GET A STATION

image of a GET request to retrieve an object with a given id

PUT REQUEST – UPDATE A STATION

http PUT request to update an object with a given ID

Notice that the object property that we want to update is included in the request body, together with the object ID. This is important in order to successfully update the object property, in this case, the name of the radio station.

DELETE REQUEST – DELETE A STATION

http DELETE request with a given ID

For the DELETE request, there is no need to include  the station object in the request body. This is because, all we need to delete an object is the ID of the object.

POST REQUEST – CREATE A STATION

http POST request with a response from the server

For a POST request, we have to include the object properties in the request body except the ID. The database generates an ID value when a POST request is successful.

HTTP Put and Delete Requests – Building Restful APIs…

In order to update or delete an information, we need have the object ID. Let's analyze the code block below for handling HTTP Put...
appdevchannel
1 min read

Validating POST Request – Building Restful APIs using Express:…

It is important to have a validation mechanism that validates inputs when building web services. Read more as we use the joi package to...
appdevchannel
2 min read

Building Restful APIs using Express: Part 6 – POST…

In this section, we will discuss how to use http POST request to create a new radio station. We will test endpoint using the...
appdevchannel
2 min read

2 Replies to “Building Restful Services using Express – Introduction”

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss a tutorial from us, get weekly updates in your inbox.