Understand NodeJs ExpressJs Routing

ExpressJs Routing

When I first started learning Nodejs, It took few days to properly understand how routes work in an ExpressJs backend. In the previous article, I discussed how to get started with Nodejs ExpressJs Application. Here, We will learn how to use NodeJs ExpressJs routing. If you are new to Nodejs then I will encourage you to complete my previous tutorial before reading this post. Routing is essential for building a sophisticated backend. With proper routing, your application backend URLs would be more professional and easy for front-end developers.

We will learn about different express methods and properties like

Prerequisites

I assume you have installed express-generator and nodemon via npm globally. So you can use express and nodemon command from your terminal.

Generate NodeJs ExpressJs project

You need to generate your project via express-generator and then install all the npm dependencies. Then we will start our app using nodemon.

Run the following commands one by one.

Now if you open your browser and go to http://localhost:3000 then you should see the default express app welcome page

Begin Express Routing

Open this project in your favourite text editor. We will create two files about.ejs and contact.ejs.

This is a normal extremely boring :p about page.

Above one is contact.ejs file. Please notice that we have two variables ( title and email ) We will pass their value from the render method.

Now our aim is to render these two files via express’s res.render method.

Open routes/index.js file. We want when people visit /about and /contact-us in their browsers, they should see respective about.ejs and contact.ejs pages. To do this we need to make two separate get routes and render their separate view files.

res.render method assumes the first argument to be the name of the view file (extensions are not needed). The second argument is the data you want to pass to that view. You may be wondering how it knows which file and where to look. Actually, in the app.js file, we are setting our view engine and also telling express where to look for the files for our views. If you open app.js, you will see these two following lines.

Now if you visit /about and /contact-us  you should see two pages like these

about

contact us

How to add Expressjs Routing parameters

If you need to use the values passed through your URLs, then you must need to use req.params in your routes callback methods. See the example below,

We have our URL like, http://localhost:3000/post/15/comment/254 then we would write or routes like

The captured values are populated in the object req.params with the name of the route parameter specified in the path as their respective keys. In our browser, we would see the result like

Like so, if you embed data in the body of your request, it would appear in the req.body object.

Using req.query

If you like to send data in the old fashion like, http://localhost:3000/getAll?hey=all&he=45&text=i%20like%20this then you need to use req.query in the following manner.
You need to append a question mark “?”  after your route path.

Visit your browser, you will see

Here are some important response methods

  • res.download() – Prompt a file to be downloaded.
  • res.json() – Send JSON response to the browser.
  • res.redirect() – Redirect to a new route.
  • res.sendFile() – Send a specific file to the browser

In this article, I only discussed the most commonly used ExpressJS route methods and properties that you must know to get started building your backend. With ExpressJS, you can make your backend much faster than any other framework. I would love to hear from you. Comment below and let me know if you have any suggestions to improve this.

Free Email Updates
Get the latest content first.
We respect your privacy.

Leave a Reply

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