Node.js Express路由命名和排序:如何确定优先级? [英] Node.js Express route naming and ordering: how is precedence determined?
问题描述
//音乐专辑
app .get('/ api / albums',routes.albums.getAlbums);
app.get('/ api / albums /:id',routes.albums.getAlbum);
app.get('/ api / albums / artwork',routes.albums.getAlbumArtwork);
,我尝试使用以下jQuery AJAX代码片段点击他们:
(click,function(){
$ .ajax({
url:$ {
$。 api / albums / artwork,
type:GET,
data:{
艺术家:$(#albumArtist)。val(),
title:$ #albumTitle)。val()
},
// ...回调等
由于某种原因,此调用使用 /:id
参数代替第二个处理程序,而不是显式 / artwork
route。如此交换它们使它们按预期的方式运行:
//音乐专辑
app.get('/ api / albums',routes.albums.getAlbums);
app.get('/ api / albums / artwork',routes.albums.getAlbumArtwork);
app.get '/ api / albums /:id',routes.albums.getAlbum);
为什么会发生这种情况? Express将足够聪明地识别一个id参数( / albums / 23453243
)与querystring( / albums / artwork?artist = artistName& title =
没有不是:id
将匹配任何内容。所以 / api / albums / artwork
对于该匹配完全有效。 Express也支持RegExp匹配。所以你可以使用RegExp创建一个明确的数字匹配路径。
另一个选项是在API文档中使用app.param作为解释
http://expressjs.com/api.html#app.param
这允许您为路由器定义匹配的参数,以便您可以使用 / api / albums /:albumId
之类的URL,其中:albumId
必须是数字的,如果你也愿意,你也可以验证一个 albumId
。
但总而言之,您正在做的第二种方式很正常,通常我将静态路由放在顶部,然后将动态路由,全部捕获,然后是错误处理程序。
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?
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 explain in the API documentation here http://expressjs.com/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.
这篇关于Node.js Express路由命名和排序:如何确定优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!