NodeJs

What is Routing? How Parameter Routing in NodeJs Express Framework works?

Route parameters and routing Express framework

In the previous NodeJs tutorials, we have introduced express and seen how we can create our first node.js application with the express framework. We have also learned about Package.json. Now in this tutorial, we are going to introduce routing and we will see that how Routing in NodeJs works with Express framework.

What is Routing in NodeJs?

Before we start it would be helpful to first understand what routing is. So routing is smoothing like application endpoint, which is a URI (or path) respond to the client request or specific HTTP request method. Basically routing is used to structure the URL’s of the web applications and it helps in defining the server routes, endpoint, and pages.

Creating the Project:

Now we are going to create a simple website to learn how routing works in Express. The first thing we need to do is to create a directory and then create a JavaScript file. After creating JavaScript file open command prompt in that directory and install express.

>npm install express

After installing express, then we need to initialize express into our project, to initialize we will create require in our JavaScript file. Here is the demonstration below:

var express = require('express');
var app = express(); 

Now we will create the server:

app.listen(3000, function() {
console.log('Hey there from the server')
}) 

Now we will create the different route for different pages like a URL for the Home page, contact page and about page.

app.get('/home', function(req, res) {
res.send('Welcome to routing demo page!');
});

app.get('/about', function(req, res) {
res.send('This is About Page!');
});

app.get('/contact', function(req, res) {
es.send('Contact Us Page!');
}); 

This code will create the different route for different pages, like home, about and contact page as you can see in below illustration.

Routing in NodeJs with Express framework

If we get the request from the URL that we have not defined then we will get the following error:

Routing using JavaScript Express Framework

To solve this problem we will create another get request for *, whenever our web application get the request from any URL aside from those three URL’s that we have already defined, we will get a page.

app.get('*', function(req, res) {
res.send('Sorry, This Page does not exist!');
});

Make sure this code always comes at the end of the all defined URL’s otherwise it will overwrite all the pages that we have defined.

URL parameters in Express.js

Now we are going to introduce route parameters. So the route parameters are the named URL which is used to capture the URL with the specified value at the position.

For example: if we want to capture the value at the URL’s ( /home/users/12345/danny ) then we will defined as (/home/users/:postID/:username). Here is the code below:

app.get('/home/users/:postID/:username', function(req, res) {
res.send('This is our Route Parameter Page:!');
});

After writing this code in your JavaScript file, then save that file and restart the server. Whatever we write the post id and username, it will redirect you to this page.

Routing in NodeJs: What is parameter routing Express framework, Nodejs

Now if we to print the username in the page then we will use the route. Params attribute to define the page name. Here is the demonstration below:

app.get('/home/users/:username', function(req, res) {
var users = req.params.username;
res.send('Welcome to '+users.toUpperCase()+' Page');
});

Now restart your server, and when we write the username it will write that username in the page. Here is the output below:

So when we go to this URL’s (http://localhost:3000/home/users/dany) then it will write “Welcome to Dany” o the page.

Parameter Routing with javascript Node js Express framework

Similarly, for this URL (http://localhost:3000/home/users/Alex), you can see we have “Welcome to Alex Page”

Parameter routing route params Javascript Nodejs express framework

Conclusion:

Now in this NodeJs tutorial, we have discussed routing in Express and discussed that how the routing works in Express application. We have also learned about URL parameters and seen how routing parameters work in the Node.Js express framework.

Click to comment

Most Popular

To Top