Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express route naming and ordering: how is precedence determined?

Say I've got a few GET routes on my Express application:

// music albums app.get('/api/albums', routes.albums.getAlbums); app.get('/api/albums/:id', routes.albums.getAlbum); app.get('/api/albums/artwork', routes.albums.getAlbumArtwork); 

and I attempt to hit them using the follow jQuery AJAX snippet:

$("#retrieveAlbumArtwork").on("click", function() {     $.ajax({         url: "api/albums/artwork",         type: "GET",         data: {             artist: $("#albumArtist").val(),             title: $("#albumTitle").val()         },         // ... callbacks and such 

For some reason, this call hits the second handler, with the /:id parameter, instead of the explicit /artwork route. Swapping them like so makes them function as expected:

// music albums app.get('/api/albums', routes.albums.getAlbums); app.get('/api/albums/artwork', routes.albums.getAlbumArtwork); app.get('/api/albums/:id', routes.albums.getAlbum); 

Can someone explain exactly why this is happening? I would assume Express would be smart enough to identify an id param (/albums/23453243) versus a querystring (/albums/artwork?artist=artistName&title=albumTitle) and route appropriately, but this doesn't seem to be the case?

like image 992
J. Ky Marsh Avatar asked Jul 18 '13 23:07

J. Ky Marsh


People also ask

What is the correct order of defining routing using Express methods are?

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 Express define routes?

A route is a section of Express code that associates an HTTP verb ( GET , POST , PUT , DELETE , etc.), a URL path/pattern, and a function that is called to handle that pattern. There are several ways to create routes.

Which is the right way to define routes in node JS?

We define the routes by using the methods of this “app” object. This app object specifies a callback function, which is called when a request is received. We have different methods in app object for a different type of request. The next() is used to hand off the control to the next callback.

How does Express work with node?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.


1 Answers

No it isn't. :id will match anything. So /api/albums/artwork is totally valid for that match. Express does support RegExp match also. So you could make an explicit numeric matching route using RegExp.

Another option is using app.param as explained in the API documentation here: https://expressjs.com/en/api.html#app.param

This allows you to define matching params for the router so you could have a URL like /api/albums/:albumId where :albumId has to be numeric, you could also validate an albumId at this point if you wished too.

But in all, the second way you are doing it fairly normal, generally I put static routes at the top, then dynamic routes, catch all, then error handlers.

like image 155
Morgan ARR Allen Avatar answered Oct 12 '22 10:10

Morgan ARR Allen