Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of router precedence in express.js

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.

like image 721
hoanganh17b Avatar asked Sep 16 '15 08:09

hoanganh17b


People also ask

What is correct order of defining routing using Express method?

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.

How does routing work in Express JS?

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.

What is Router () in Express?

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 router route in node 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.


1 Answers

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'     }); }); 
like image 171
Laxmikant Dange Avatar answered Sep 18 '22 04:09

Laxmikant Dange