Restful APIs using Express: Part 4 – Route Parameters

1 min read

In the second part of this series, we developed our first Restful API. We implemented an endpoint that responds with a welcome message whenever there is an http GET request to the base url. Also, we built a second endpoint for retrieving the list of radio stations stored in array.

In this tutorial, we will discuss about route parameters, as well as how we can use query strings in express application. We will build another endpoint for retrieving a single radio station from a list of radio stations in an array. In order to achieve this, we need to include the id of the radio station in our endpoint. Refer to the first part of this tutorial, for a quick revision.

Next, let’s implement the route for retrieving a single radio station, by adding the code snippet below to index.js file.

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

The id is the name of the parameter. In order to read the parameter, we make use of req.params.id. The res.send(req.params.id) in the code block above is used to send the id to the client.

image of how to send route parameter from server to client in node express app

It is also possible to have multiple parameters in a route. For instance, in a blog application with a post date (year, day and month).

/api/v1/posts/:year/:month/:day

In order to get the request parameter object, we need to pass the year, month and day of the post. The resulting object properties should look like:

image showing multiple route parameters in express API

Query Strings

It is possible to retrieve query strings parameters when working with the Express framework. Query string parameters are used for providing additional data for web services. For example, let’s consider the url below.

/api/v1/posts/2018/11?sortBy=name

The query string is the parameter after the question mark(?). This means that all the post for the month of November, 2018 will be arranged by name.

In summary, Route parameters are used for retrieving required values, while Query string parameters are used for retrieving optional information.

To read a query string parameter, we need to make use of req.query.

app.get('/api/v1/posts/:year/:month', (req, res) => {
    res.send(req.query);
});

In other to test the response from the server when we sort all post in a month by name, let’s open up the browser and type the url below.

localhost:5000/api/posts/2018/11?sortBy=name

The response object from the server should be the query parameter we sent using res.send(req.query) on line 18, in the code snippet below.

image showing a response with query parameter

The code below is an update to the index.js file in third part of this tutorial series.

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

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.