简体   繁体   English

具有命名参数的Expressjs路由

[英]Expressjs routes with named parameters

Im struggling a bit with named parameters. 我在命名参数方面有些挣扎。 Assuming the following: 假设以下内容:

app.get('/names/:name', function (request, response) {
  …
});

Is a legitimate route that will response to requests such as /names/buddy123 是一条可以响应诸如/names/buddy123请求的合法路由

Another legitimate route is: 另一条合法途径是:

app.get('/names/age', function (request, response) {
  …
});

That will response to requests and would probably return a list of all available ages. 这将响应请求,并可能返回所有可用年龄的列表。

Each route is well defined when is defined alone but once both exist, age is considered a name and requests arent routed as I'd expect. 当单独定义每条路线时,定义都很好,但是一旦两者都存在, age被视为一个名称,并要求按我期望的路线进行布置。

Is there a way to overcome this? 有办法克服吗?

Try to define your age route before the named one: 尝试在指定的路线之前定义您的age路线:

app.get('/names/age', function (request, response) {
  …
});

app.get('/names/:name', function (request, response) {
  …
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM