我已经实现了带有递归参数的 react 路由器。类似 this
我有一个带有express的服务器,我尝试在其中处理路由/someRoute/:recursiveParameter?
router.get('/someRoute/:recursiveParameter?', async (req, res, _next) => { whatever here })
但我无法捕获具有更多参数的路线,例如。
localhost://someRoute/param1/param2
我找不到任何解决方案。另外,我无法编写像 someRoute/:param1/:param2 这样的路由,因为我不知道会有多少个参数。
如何通过 Express route 的未知动态参数数量来解决此问题?谢谢
最佳答案
在路由中使用 * 等通配符,它还支持正则表达式,工作示例:
app.get('/someRoute/*', function(req, res) {
console.log(req.params[0]);
});
// GET /someRoute/v2/this/route
// Output: v2/this/route
关于javascript - Express 服务器中的无限参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57670931/