简体   繁体   English

express.js链路以任何顺序排列

[英]express.js chain routes in any order

I'm making an API in express that has many endpoints, a few examples: 我正在快递中创建一个具有许多端点的API,例如:

product/colour/:COLOUR
product/brand/:BRAND
product/size/:SIZE
product/gender/:GENDER

The thing is, I want the user to be able to chain such endpoints in whichever order they please, for example one user may type this endpoint: 问题是,我希望用户能够以他们喜欢的顺序链接这些端点,例如,一个用户可以键入此端点:

product/size/:SIZE/colour/:COLOUR/gender/:GENDER/

but another user may type 但另一个用户可能会打字

product/gender/:GENDER/size/:SIZE/colour/:COLOUR

Is there a way to do this without creating hundreds upon hundreds of routes for each possible combination? 有没有办法做到这一点,而不是为每种可能的组合创建数百条路线?

Thanks in advance, Ant 提前谢谢,Ant

可能是查询字符串适合这种情况。

/product?colour=red&size=xl&gender=male

all your defined routers will be not matched. 您定义的所有路由器都不匹配。

You can use this route to match all you need, 您可以使用此路线匹配您所需的一切,

product/:key1/:value1/:key2/:value2/:key3/:value3

and then you can use 'if' to check your key/value pair at controller path 然后你可以使用'if'检查控制器路径上的键/值对

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

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