const express = require('express'); const app = express(); app.get('/users/profile', (req, res) => { res.send('User profile route'); }); app.get('/users/:id', (req, res) => { res.send('User ID route'); });
The route '/users/profile' matches the path '/users/profile' exactly.
Since it is defined before '/users/:id', it handles the request for '/users/profile' first.
For '/users/123', the '/users/:id' route matches.
const express = require('express'); const app = express(); app.get('/about', (req, res) => { res.send('About page'); }); app.get('/:page', (req, res) => { res.send('Page: ' + req.params.page); });
The '/about' route is defined before the dynamic '/:page' route, so it matches first and responds correctly.
No syntax or runtime errors occur.
const express = require('express'); const app = express(); app.get('/:page', (req, res) => { res.send('Page: ' + req.params.page); }); app.get('/contact', (req, res) => { res.send('Contact page'); });
The dynamic route '/:page' matches '/contact' first because it is defined before the specific '/contact' route.
Therefore, the response is 'Page: contact'.
const express = require('express'); const app = express(); app.get('*', (req, res) => { res.send('Catch-all'); }); app.get('/home', (req, res) => { res.send('Home page'); }); app.get('/about', (req, res) => { res.send('About page'); });
The route with '*' matches every path and is defined first, so it handles all requests.
Routes defined after it are never reached.
Specific routes like '/products' should be defined before dynamic routes like '/:category' to ensure they match first.
Otherwise, the dynamic route will catch requests intended for the specific route.