ExpressJS Router is a class that acts as a middleware to provide route handling or determining how an application responds to a client requests, of various HTTP methods, to a particular URI.
Creating Express App and Installing Module:
Step 1: Create package.json using the following command:
npm init
Step 2: You can visit the link Install Express to see how to install the express module. You can install this package by using the command :
npm install express
Step 3: Create app.js, params.js, and routes.js files in the view folder. The project directory will look like this:

Project Structure
Setup Regex in URL:
We can easily set up Regex for our Express Router by following these two points:
- To make our route match with a particular regular expression, we can put regex between two forward slashes like this /<routeRegex>/
- Since every route contain /, so wherever we have to use / inside Regex, use a backslash \ / before it.
Example: In the following code, we are setting up regex, for when HTTP get request is received to route /home, a response Homepage is rendered to the client.
router.get(/\/home/ , (req,res)=>{
res.send('Homepage');
)
Now create a routes.js file that will contain different requests for different routes using Regex.
routes.js
const express = require( 'express' );
const router = express.Router();
router.get(/\/a[b|d]c/, (req, res) => {
res.send( "<h1>Route First</h1" );
})
router.get(/\/a[0-9]\/c{2,3}/, (req, res) => {
res.send( "<h1>Route Second</h1" );
})
router.get(/^\/Hello[a-z]*OK$/, (req, res) => {
res.send( '<h1>Route Third</h1>' )
})
router.get(/\/*Hello$/, (req, res) => {
res.send( '<h1>Route Fourth</h1>' )
})
module.exports = router;
|
Setting up Regex in URL parameters:
To set up Regex for URL parameters, we can provide regex inside parenthesis just after the name of the parameters. In the following file, we have implemented Regex for URL parameters.
params.js
const express = require( 'express' );
const router = express.Router();
router.get( '/user/:name([a-zA-Z]+)/:contact([6-9][0-9]{9})' , (req, res) => {
const name = req.params.name;
const contact = req.params.contact;
res.send({
"username" : name,
"contact" : contact
})
})
module.exports = router;
|
app.js
const express = require( 'express' );
const app = express();
const router = express.Router();
const route = require( './routes' );
const param = require( './params' );
app.use( '/' , route);
app.use( '/' , param);
app.listen(8000, () => {
console.log( "Server is listening on port 8000" );
})
|
Step to run the application: Run the app.js file using the following command.
node app.js
Output:
- Showing the functioning of all the routes defined in the routes.js file.

Execution of routes.js
- Showing the functioning of the routes defined in the params.js file.

Execution of params.js
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!