We now have a youtube channel. Subscribe!

Express.js | URL Building

Express.js | URL Building


Hello folks! Welcome back to a new edition of our tutorial on ExpressJS. In this tutorial, we will be discussing about URL Building in ExpressJS.

We can now define routes, but those are static and fixed. To use dynamic routes, we should give different types of routes. Using dynamic routes lets us to pass parameters and process based on them.

Example

Following is an example of dynamic routes -

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

app.get('/:id', function(req, res){
   res.send('The id you specified is ' + req.params.id);
});
app.listen(3000);

Output

To test this go to http://localhost:3000/123. The following output will be displayed.

url_building_id

You can replace 123 in the url with anything else and the change is going to reflect in the response. A more complicated example of the above is -

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

app.get('/things/:name/:id', function(req, res) {
   res.send('id: ' + req.params.id + ' and name: ' + req.params.name);
});
app.listen(3000);

Output

To test this, open up your browser and go to localhost:3000/things/webdesigntutorialz/123.

url_building_complex

You can make use of the req.params object to access all the parameters you pass in the url. Note that the above two examples are different paths. They will never overlap. Also if you want to execute a code when you get '/things', you need to define it separately.


Pattern Matched Routes

You can also make use of regex to restrict URL parameter matching. Let's assume you need the id to be a 5-digit long number. You can use the following route definition -

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

app.get('/things/:id([0-9]{5})', function(req, res){
   res.send('id: ' + req.params.id);
});

app.listen(3000);

Note that this will only match the requests that has a 5-digit long id. You can make use of more complex regexes to match/validate your routes. If none of your routes matches the request, you'll get a "Cannot GET <your-request-route>" message as response. This message can be replaced by 404 not found page using this simple route -

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

//Other routes here
app.get('*', function(req, res){
   res.send('Sorry, this is an invalid URL.');
});
app.listen(3000);

Note - This should be placed after all your routes, as ExpressJS matches routes from start to end of the index.js file, including the external routers you required.

For example, if we define the same routes as above, on requesting with a valid URL, the following output is displayed -

url_matching_correct

While for incorrect url request, the following output will be displayed.

url_pattern_invalid


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about Express.js Middleware.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain