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:
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.
// Requiring module const express = require( 'express' );
const router = express.Router(); // Route which matches /abc or /adc router.get(/\/a[b|d]c/, (req, res) => { res.send( "<h1>Route First</h1" );
}) // Routes that matches /a(any single digit)/ // followed by 2 times c or 3 times c or // /a(any single digit) / followed by 2 // times c or 3 times c router.get(/\/a[0-9]\/c{2,3}/, (req, res) => { res.send( "<h1>Route Second</h1" );
}) // Routes that ends with /Hello followed by // a letter in [a-z] any no. of times and // ends with "OK" router.get(/^\/Hello[a-z]*OK$/, (req, res) => { res.send( '<h1>Route Third</h1>' )
}) // Routes that must end with string "Hello" // and can have any no. of any character // before that 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.
// Requiring module const express = require( 'express' );
const router = express.Router(); // Setting up regex for name and contact parameters 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; |
// Requiring modules const express = require( 'express' );
const app = express(); const router = express.Router(); const route = require( './routes' );
const param = require( './params' );
// Using routers as middleware to // use route.js and params.js app.use( '/' , route);
app.use( '/' , param);
// Starting server on port 8000 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.
- Showing the functioning of the routes defined in the params.js file.