Node.js Routing

Welcome to Part 5 of this review of the Pluralsight course Building Web Applications with Node.js and Express 4.0 by Jonathan Mills.


Jonathan is a JavaScript and Node.js expert working mostly in the MEAN Stack with individuals and companies to help build their technical skills to cope with the constantly changing landscape of software development.

He is also an ASP.NET insider and an international speaker focusing on JavaScript both in the browser and on the server.

In this course we’re building a library application that’s going to keep track of a list of books. In this module we’re doing book routes and author routes.

Also in this series:

Part 1 – Getting Started
Part 2 – Setting up Express
Part 3 – Setting Up Gulp
Part 4 – Templating Engines
Part 5 – Routing


Jonathan recaps the work we did in earlier in the course. In app.js we change one of our res.render function to use

nav: [{Link: ‘/Books’, Text:’Books’}, {Link: ‘/Authors’, Text:’Authors’}]

Next see see how to update our index.ejs to display this data as HTML. (If you’ve not heard of EJS before see the Template Engines module).

A minute later we have HOME, BOOKS and AUTHORS pages showing in our site.


In this lesson we use an Express router:

var bookRouter = express.Router();

bookRouter.route(‘/Book’).get(function(req, res){
res.send(‘Hello Books’);

This lets us encapsulate all of the books associated with /Books all in one route, and in this lesson we create two different routes.


We start with a new view books.ejs which is based on index.ejs.

We update app.js to try to render books. It’s initially broken, and after an update to the parameter although we see our site again we don’t see any books.

So we create a books array. As we’re not yet worrying about databases we hard-code our book details for now.

After some EJS updates we have our books displayed in our web page.

But there is a LOT of code in app.js just for doing this very simple site. We should split this out.

Separate Files

We create bookRoutes.js add our code and export it as a module.

Single Book Route

Implementing the ability to show only a single book instead of a collection of books.

Rendering a Single Book

We create bookView.ejs. The whole process has been involving lots of copy-pasting and it all feels like poor engineering. Jonathan says we’ll soon start breaking this up into partial views.

The work in this lesson mainly involves removing the array indexers from our book object.

Router Functions

Again we’re hard-coding stuff instead of pulling it in from the database.

We create a function that takes a nav parameter and putt our block of code inside here. This is parameterizing our route.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s