如何在express.js中定义具有命名参数的复杂路由? [英] How to define complex routes with named parameters in express.js?

查看:100
本文介绍了如何在express.js中定义具有命名参数的复杂路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一些更复杂的路由,可以包含多个可选参数。最好,我想使用命名参数来方便访问。以下是我要实现的一个例子:

  //以下路由应根据提供的参数进行匹配: 
// GET / books /:category /:author
// GET / books /:category /:author / limit /:limit
// GET / books /:category /:author / skip /:skip
// GET / books /:category /:author / limit /:limit / skip /:skip
// GET / books /:category /:author / sort /:sortby
// GET / books /:category /:author / sort /:sortby / limit /:limit
// GET / books /:category /:author / sort /:sortby / skip /:skip
// GET / books /:category /:author / sort /:sortby / limit /:limit / skip /:skip
app.get('/ books /:category /:author / /:sortby)?/(limit /:limit)?/(skip /:skip)?',myController.index);

正如你所看到的,我试图将可选参数与 (paramKey / paramValue)?。这样,我希望能够在使用参数命名的同时混合和匹配可选参数。不幸的是,这似乎不起作用。



有没有办法得到这个工作,而不必编写直接的正则表达式,并添加额外的逻辑来解析任何生成的索引 - 基本上,我正在寻找一种简单的方法来解析路由中的键/值对。

解决方案

您希望使用URL路径重新实现查询字符串。我的猜测是,如果你真的想要的话,是的,你必须编写自己的解析/解释逻辑。 AFAIK快速路径参数是位置,而查询字符串不是。只需使用查询字符串,表达将自动解析。

  / books?category = sports& author = jack& limit = 15?sortby = title 

这将使您能够执行

  req.query.sortby 

你可以使用正常的表达式路径(如(:key /:value)* 或者沿着这些行的某些方式来获得快速的解析效果(即将匹配多个键/值对),但表达不会再为您解析这些结果。


I want to set up some more complex routes that can include a number of optional parameters. Preferably, I would like to use named parameters for ease of access. Here is an example of what I'm trying to achieve:

// The following routes should be matched based on parameters provided:
// GET /books/:category/:author
// GET /books/:category/:author/limit/:limit
// GET /books/:category/:author/skip/:skip
// GET /books/:category/:author/limit/:limit/skip/:skip
// GET /books/:category/:author/sort/:sortby
// GET /books/:category/:author/sort/:sortby/limit/:limit
// GET /books/:category/:author/sort/:sortby/skip/:skip
// GET /books/:category/:author/sort/:sortby/limit/:limit/skip/:skip
app.get('/books/:category/:author/(sort/:sortby)?/(limit/:limit)?/(skip/:skip)?', myController.index);

As you can see, I'm trying to group the optional parameters with (paramKey/paramValue)?. That way I was hoping to be able to "mix and match" the optional parameters while still making use of the parameter naming. Unfortunately, this doesn't seem to work.

Is there any way to get this working without having to write straight regular expressions and adding additional logic to parse any resulting index-based parameter groups?

Basically, I'm looking for an easy way to parse key/value pairs from the route.

解决方案

It looks like you want to re-implement the query string using the URL path. My guess is that if you really wanted that, yes, you would have to write your own parsing/interpreting logic. AFAIK express path parameters are positional, whereas the query string is not. Just use a query string and express will parse it for you automatically.

/books?category=sports&author=jack&limit=15?sortby=title

That will enable you to do

req.query.sortby

You may be able to get express to do 1/2 of the parsing for you with a regular expression path like (:key/:value)* or something along those lines (that will match multiple key/value pairs), but express isn't going to then further parse those results for you.

这篇关于如何在express.js中定义具有命名参数的复杂路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆