Home » Node.js

Route parameters in Express.js (Node.js)

In this article, we are going to learn how to route through parameters in Node.js?
Submitted by Manu Jemini, on November 27, 2017

This is a simple but necessary topic for anyone learning Exprees.js. The first thing to do is to make sure you know a little about routing and Middleware, both these we have diligently covered.

Let’s move to a little deeper in parameters. Parameters are values which we want in order execute some task designed. These parameters in some cases and in others they are not. I will discuss both in details. When we are talking about parameters, there is always some kind confusion about whether to use,


Well, both works the same and it depends on which you like more. The first approach is quite stricter while other is not. This makes the first technique a little safer while the other an error-prone. I prefer second technique and would recommend you to do so.

Let’s see how both works separately for better understanding?

http://localhost:3000/hello/Includehelp - For this we have to do something like,

app.get('/hello/:name', function(q,s){
	console.log("hello " +req.params.name);

Now, for second approach, we have

http://localhost:3000/hello?name=Includehelp - For this we have to do something like,

app.get('/hello', function(q,s){
	console.log("hello " +req.query.name);

Both yield the same result. It’s up to you which one you like more.

The First approach makes it less error-prone if you need the parameter utmost.

The Second has two benefits, its make your coding a little easier as you want to keep the pattern clean and also if it not necessary for you to have the parameter as you operate with or without it. Its common example is when are making a single generic function for two quite different tasks. Both the task will send the different pair of parameters but you still can track through them and by-passing the other set.

Even you don’t need to have two different tasks but will do better with simple and single. Let’s keep anyone.

These all yield the exact same result.

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.