I would like to understand the order precedence in express.js. For example, as bellow code
var routes = require('./routes/index'); var users = require('./routes/users'); var api = require('./routes/api'); app.use('/api', api); app.use('/users', users); app.use('/:name', function(req, res, next) { console.log('from app.js name:', req.params.name); res.render('index', { title: req.params.name }); }, routes); app.use('/', function(req, res, next) { res.render('index', { title: 'MainPage' }); });
If a request come from client localhost:3000/api/abc and localhost:3000/user/abc, the response from api and user module. But if I make a request like localhost:3000/myName/xyz, the app module return a response. This behavior let me concern about what is precedence of expressjs and what is correct order for router modules. Why routers do not confuse between actions "api", "users" and parameter ":name". Please let me understand clearly how express does and what is precedence.
The order is first come first serve. In your case, if user hits /api, he will get response from api, but if you write /:name route before /api , /:name will serve for /api requests also.
A route method is derived from one of the HTTP methods, and is attached to an instance of the express class. The following code is an example of routes that are defined for the GET and the POST methods to the root of the app. Express supports methods that correspond to all HTTP request methods: get , post , and so on.
The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express. js.
What is routing? To start with routing in Node. js, one needs to know what is routing and its purpose. The route is a section of Express code that associates an HTTP verb (GET, POST, PUT, DELETE, etc.), an URL path/pattern, and a function that is called to handle that pattern.
The order is first come first serve.
In your case, if user hits /api, he will get response from api, but if you write /:name
route before /api
, /:name
will serve for /api
requests also.
Case1:
/api
will serve requests for/api
.
var routes = require('./routes/index'); var users = require('./routes/users'); var api = require('./routes/api'); app.use('/api', api); app.use('/users', users); app.use('/:name', function(req, res, next) { console.log('from app.js name:', req.params.name); res.render('index', { title: req.params.name }); }, routes); app.use('/', function(req, res, next) { res.render('index', { title: 'MainPage' }); });
Case2:
/:name
serves requests for/api
and/users
var routes = require('./routes/index'); var users = require('./routes/users'); var api = require('./routes/api'); app.use('/:name', function(req, res, next) { console.log('from app.js name:', req.params.name); res.render('index', { title: req.params.name }); }, routes); app.use('/api', api); app.use('/users', users); app.use('/', function(req, res, next) { res.render('index', { title: 'MainPage' }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With