用于匹配尾部斜杠的 Compojure 正则表达式
也许我只是个白痴,但我无法在 Clojure 中为可选的尾部斜杠设置匹配。
lein repl
REPL started; server listening on localhost port 47383
user=> (use 'ring.mock.request 'clout.core)
nil
user=> (route-matches "/article/" (request :get "/article/"))
{}
user=> (route-matches "/article/?" (request :get "/article"))
nil
user=> (route-matches "/article/?" (request :get "/article/"))
nil
user=> (route-matches #"/article/?" (request :get "/article/"))
java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route found for class: java.util.regex.Pattern (NO_SOURCE_FILE:0)
我可以使用什么正则表达式来匹配 Compojure 中的可选尾部斜杠?
Maybe I'm just an idiot, but I can't set up a match for an optional trailing slash in Clojure.
lein repl
REPL started; server listening on localhost port 47383
user=> (use 'ring.mock.request 'clout.core)
nil
user=> (route-matches "/article/" (request :get "/article/"))
{}
user=> (route-matches "/article/?" (request :get "/article"))
nil
user=> (route-matches "/article/?" (request :get "/article/"))
nil
user=> (route-matches #"/article/?" (request :get "/article/"))
java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route found for class: java.util.regex.Pattern (NO_SOURCE_FILE:0)
What regex can I use to match an optional trailing slash in Compojure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
clout
期望作为route-matches
的第一个参数的路径字符串不是正则表达式,而是可以包含关键字和*
的字符串通配符。我相信 clout 本身并不支持定义忽略尾部斜杠的路由。您可以使用删除尾部斜杠的中间件函数来解决该问题。以下函数取自旧版本的
compojure
源代码(在大重构之前),我无法确定它们是否移动到了新的地方。这是引入这些函数的原始提交。The path string expected by
clout
as the first argument toroute-matches
is not a regex, but a string that can contain keywords and the*
wildcard.I believe
clout
doesn't natively support defining routes that ignore a trailing slash. You could solve the problem with a middleware function that removes trailing slashes. The following functions were taken from an old version of thecompojure
source code (before the big refactoring), I couldn't find out if they moved to a new place. Here is the original commit that introduced these functions.这是没有依赖项的中间件的精简版本:
欢迎错误修复编辑。
Here's a condensed version of the middleware with no dependencies:
Bug fix edits welcome.
对于那些寻找更压缩解决方案的人:)
For those looking for an even more compressed solution :)