

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Handling different routes in express.js
For handling different routes, use() function is used. use() function has multiple overloaded version, one of version also takes url path as a argument. Based on the url path, the requests will be filtered out for respective middleware.
const http = require('http'); const express = require('express'); const app = express(); app.use('/', (req, res,next)=>{ console.log('first middleware'); res.send('<h1> first midleware: Hello Tutorials Point </h1>'); }); const server = http.createServer(app); server.listen(3000);
in above example we used ‘/’ as url path, it’s a default.
Now, as every route starts with ‘/’, the above middleware executes for every http request. It works for ‘/’ and for ‘/username’ as well.
To avoid above problem , we will have to use path specific middleware first before default middleware.
const http = require('http'); const express = require('express'); const app = express(); app.use('/username', (req, res,next)=>{ res.send('<h1> My username </h1>'); }); app.use('/', (req, res,next)=>{ console.log('first middleware'); res.send('<h1> first midleware: Hello Tutorials Point </h1>'); }); const server = http.createServer(app); server.listen(3000);
Now, we used ‘/username’ path middleware first and as we have not used next() in the it , it will not pass the http request to next middleware.
So we see browser output like below for different urls −
localhost:3000
For localhost:3000/username
If any pre-processing on http request requires for all, the using default middleware url path ‘/’ can be useful. Just we will have to use next() in it to pass the request after pre-processing to next middlewares.
const http = require('http'); const express = require('express'); const app = express(); app.use('/', (req, res,next)=>{ console.log('log request info', req); next(); }); app.use('/username', (req, res,next)=>{ res.send('<h1> My username </h1>'); }); app.use('/', (req, res,next)=>{ console.log('first middleware'); res.send('<h1> first midleware: Hello Tutorials Point </h1>'); }); const server = http.createServer(app); server.listen(3000);
- Related Questions & Answers
- Bus Routes in C++
- Understanding the different error types and handling in Node.js
- Adding 404 page in express
- Adding middleware in Express in Node.js
- Integrating Express-rate-limit in Node.js
- How to use express router
- Using post request in middleware in express
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- What are the different ways of handling authentication popup window using Selenium?
- Least Operators to Express Number in C++
- How OSPF routes the packets from source to destination?
- SVG morphing in React JS
- Compare express contract and implied contract
- XHTML and D3.js
- String Handling in Java