The Basics Of Express Routes | Modern Web – Web3, Business & Technology
By Dhananjay Kumar
Recently I started learning how to build web applications using Node.js and the popular Express framework. In doing so, I realized that one of the most vital concepts in Express is routes. In this article I will share the basics of understanding routes in Express.
If you are just starting with Express and nee help getting it set up, check out my other tutorial on how to set up Express and run your first application in Node.js .
Nội Dung Chính
Setting Up Your First Route
Let us assume that you have set up a basic site using Express. In root folder you will find App.js
file. When youopen App.js, you’ll see it defines two modules, express and http, to be imported along with some other modules.
var express = require('express');
var http = require('http');
var app = express();
Express uses HTTP verbs to perform routing. Thus the method names reflect their corresponding HTTP verb. Below I show the basic route methods tha you’ll use most frequently (though Express supports a bunch more):
Now that the express and http modules are imported and our Express app initialized, we can create a basic route, as below:
app.get('/',function(req,res)
{
res.send("Route path at base address");
});
This route can be accessed using HTTP GET via the base site address. As you can see in the screenshot below, we receive a plain text response object from our Express application.
In Express if you have two route paths with same address, rather than throwing an error, it will pick theroute path that was defined first in App.js
.
Passing Parameters to a Route
Let’s assume you want to pass a parameter while doing HTTP GET operation with route we defined earlier. You have two choices.
The first is to create two routes as shown below:
app.get('/',function(req,red)
{
res.send("I am foo");
});
app.get('/:id',function(req,res)
{
res.send("I am Foo with id " + red.params.id);
});
The second route will be accessed when a parameter is passed while performing a HTTP GET. So if you access application as in the screenshot below, you will get served via the second route path.
However, you may want to create only one route path where the id
parameter is optional. To do this, specify it as : id?
You can see an example in the route defined below:
app.get('/:id?',function(req,res)
{
if(req.params.id)
{
res.send("I am Foo with id " + req.params.id);
}
else
{
res.send("I am Foo");
}
});
This route can handle both types of requests, either with a parameter or without one. We are checking for the parameter within the code inside the route definition.
More Complex Paths
Express routes can be strings or regular expressions. Input parameters are parsed and accessible via req.params
, for example req.params.id
where id
is the name of the parameter.
You can pass more than one parameter as shown below. While the id
is expected, we are making op
optional by adding ?
app.get(':id/:op?',function(req,res)
{
if(req.params.op)
{
res.send("I am Foo with id " + req.params.id + " operation " + req.params.op);
}
else
{
res.send("I am Foo" + req.params.id);
}
});
Below you can see the response when we pass both id
and op
.
Express allow you to use a wildcard within a route path using *
. For example, the path defined below can be access using anything that follows the base URL (for example, if you wanted to build a “catch all” that caught routes not previously defined).
app.get('/*',function(req,res)
{
req.send("I am Foo");
});
The following URL will be caught by this route.
Normally if you try to access a route which is not defined, you will get error message like the one below. In this example, the loo
route is not defined and there is no catch all route like the one above.
Other Route Types
You can create routes with other HTTP verbs . As mentioned earlier, the method names are matched with their corresonding HTTP verbs.
app.get('/',function(req,res)
{
res.send("I am Foo");
});
app.put('/',function(req,res)
{
res.send(req.body);
});
app.post('/',function(req,res)
{
res.send(req.body);
});
app.delete('/',function(req,res)
{
res.send(req.body);
});
You can test other operations like PUT , DELETE , POST in Fiddler.
Where To Go From Here
For further advanced reading you may want to prefer my other posts on creating a REST API using Express . If you want to create routes using crossroads then you can read my posts on how to create a Router in Node.js using crossroads.
Azat Mardanov also has a couple of posts on this site cover Express Fundamentals and Express error handling and middleware.
A version of this article was originally published at https://debugmode.net/2014/03/31/understanding-routing-of-expressjs-in-node-js/