Restful APIs using Express: Part 5 – GET Request

1 min read

In order to build an endpoint to retrieve an object, for example, getting a single radio station from our online radio application, we will define an array of radio stations with three objects. Each objects will have the property id and a name.

const stations = [
    {id: 1, name: 'Dublab'},
    {id: 2, name: 'BBC-Radio'},
    {id: 3, name: 'KAMP-FM'}
];

Getting the List of Radio Stations

To retrieve the list of radio stations, we need to write a code block as shown below. Refer to Part 2 of this tutorial series for more details.

app.get('/api/v1/stations', (req, res) => {
   res.send(stations);
});

This means that the array of stations defined above will be returned when there is a GET request to the endpoint with stations resources.

Getting a Single Radio Station

app.get('/api/v1/stations/:id', (req, res) => {
    const station = stations.find(s => s.id === 
    parseInt(req.params.id));
    
    if (!station) return res.status(404).send(`The Radio station 
    with id: ${id} not found`);
    
    res.send(station);
});

Analyzing the code block above, you will notice that an arrow function was passed as argument to the find method. The find method is a method available on every array in Javascript. We used the arrow function on line 21 to find a radio station that matches a given criteria. The logic simply returns a boolean value. This determines whether the radio station is the one one we are looking for or not.

Also, the req.params.id on line 21 returns a string. Therefore, for our comparison logic to work properly, we simply interpret the string into an integer, using the javascript global function called parseInt.

Finally, we stored the result of the find operation in a constant called station.

It’s possible that a client application might request a radio station with an id that isn’t available in the database. Whenever this happens, the ‘if‘ statement on line 23 – 26 successfully handles the situation by using a standard http response with a status code of 404. In other words, the object requested wasn’t found.

image showing a request with an id not available in database

If we find a station with the given id, we will return the radio station to the client app. This is the function of the code on line 28.

image of an object information with a found ID from express server

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 “Restful APIs using Express: Part 5 – GET Request”

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.