4elements

blog

Understanding ExpressJS Routing

Introduction

Express is a popular Node.js web framework. Amongst its functionality, it provides a wrapper for Routing. The Express Router helps in the creation of route handlers. In this tutorial, you will learn how to work with Express Router.

Let's get started.

Create a new directory for your application. Run the command to initialize npm in the directory you just created.

npm init -y

The only dependency you will need is express, so go ahead and install it.

An instance of Express Router is created. The instance is set to a variable called router. This variable will be used whenever you want to create a route.

A new route is defined for the GET method to the root of the application. It is attached to the instance of the Express Router class.

A new route is defined for the POST method to the contact page of the application. It is attached to the instance of the Express Router class.

This mounts some middleware that will be used to handle your routes. Here you tell your app that you want to make use of the router (which is the instance of the Express Router) for every request made to the app that corresponds to the path '/'. If you fail to mount a path on this middleware, it will be executed for every request made to the app.

Let's say you had the code below instead.

app.use('/user', router)

This will match all the following: /user/profile, user/profile/edit, user/dashboard/article/view, and so on.

Route Methods

In the above code, you attached a route method to an instance of Express Router. The route method is derived from one of the HTTP methods and attached to the instance of the Express Router as you did.

Route Parameters

These are used to capture values that are specified at a certain position in the URL. They are called URL segments. The values captured are made available in the req.params object, using the name of the route parameter specified in the path as the keys of the values.

Here is an example.

If you have a route path similar to this in your application: /users/:userId/articles/:articleId

The name of the route parameters must be made up of word characters ([A-Za-z0-9_]).

Let's take it further!

Say you want to have a route path called /users/:name, where the name of the user is passed into the URL and the application displays the name along with a string. How do you think that can be achieved?

Login Routes

Let's see how to create a login route in Express. Your login routes require two actions on a single route path. The actions will be differentiated by the route method used. Here is how it will look.

router.get('/login', (req, res) => {
res.send('This is should lead to the login form')
})
router.post('/login', (req, res) => {
res.send('This is used in processing the form')
})

After doing this, your store form should have an action whose value is the route defined with the HTTP POST method. Here is how it should look.

<form action="/login" method="POST">
</form>

When the submit button of the form gets clicked, the specified router gets called. The difference between both route paths as stated above is the HTTP POST. This is how the application determines which is responsible for handling the data passed through the form.

Another aspect where this can be experienced is in the area of editing and updating resources.

app.route()

app.route() can be used to create a chain of route handlers for a specific route path.

Using the example of the login route, here is how you will make use of app.route().

app.route('/login')
.get((res, req) => {
res.send('This is should lead to the login form')
})
.post((res, req) => {
res.send('This is used in processing the form')
})

You can add more route handlers than we have above.

Conclusion

At this point, you should know how routing works in Express. You have learned how to set up basic routing, and also how to work with route methods and paths. You saw how to make use of route parameters and retrieve values sent via the URL.

If you’re looking for additional JavaScript resources to study or to use in your work, check out what we have available on Envato Market.

With this knowledge, you can go further to build an Express application with complex routing.