快速路线参数条件 [英] Express routes parameter conditions

查看:112
本文介绍了快速路线参数条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Express应用程序上有一条路线,如下所示:

  app.get('/:id'函数(请求,响应){
...
});

ID始终是一个数字。但是,目前这条路线与其他事情相匹配,例如 / login



我想我需要两件事:


  1. 只能使用如果ID是一个数字,并且

  2. ,只有当该特定参数没有定义的路由(例如与 / login

可以这样吗?

解决方案

扩展Marius的答案,您可以提供正则表达式和参数名称:

 应用程序.get('/:id(\\d +)/',function(req,res){
// req.params.id这里定义为
});


I have a route on my Express app that looks like this:

app.get('/:id', function (request, response) {
  …
});

The ID will always be a number. However, at the moment this route is matching other things, such as /login.

I think I want two things from this:

  1. to only use this route if the ID is a number, and
  2. only if there isn't a route for that specific paramater already defined (such as the clash with /login).

Can this be done?

解决方案

Expanding on Marius's answer, you can provide the regex AND the parameter name:

app.get('/:id(\\d+)/', function (req, res){
  // req.params.id is now defined here for you
});

这篇关于快速路线参数条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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