快递中路线匹配的正则表达式 [英] Regex for route matching in Express

查看:401
本文介绍了快递中路线匹配的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用正则表达式不是很好,所以我想确保我正确地这样做。假设我有两条非常相似的路线: / discussion /:slug / / page /:slug / 。我想创建一个匹配这两个页面的路由。

I'm not very good with regular expressions, so I want to make sure I'm doing this correctly. Let's say I have two very similar routes, /discussion/:slug/ and /page/:slug/. I want to create a route that matches both these pages.

app.get('/[discussion|page]/:slug', function(req, res, next) {
  ...enter code here...
})

这是正确的方法吗?现在我只是创建两条独​​立的路线。

Is this the correct way to do it? Right now I'm just creating two separate routes.

someFunction = function(req, res, next) {..}
app.get('/discussion/:slug', someFunction)
app.get('/page/:slug', someFunction)


推荐答案

app.get('/:type(discussion | page)/:id' 。) works

这篇关于快递中路线匹配的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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