简体   繁体   English

Express.js:路由中的正则表达式

[英]Express.js : regex in route

I want to get a value in my url. 我想在我的网址中获取一个值。

My url is like : 我的网址是这样的:

host/:value.schema 主机/:value.schema

I want to get value. 我想获得价值。

Exemple : 范例:

host/horse.schema value = horse 主机/马。模式值=马

I have another route without .schema: 我还有一条没有.schema的路线:

host/:value 主机/:值

Exemple : 范例:

host/horse value = horse 主机/马的值=马

How tell Express to make the difference ? 如何告诉Express有所作为?

You can try something like this: 您可以尝试如下操作:

app.get('/:value.:schema?', function (req, res) {
  console.log(req.params);
});

You'll receive this: 您会收到以下信息:

http://localhost:3000/horse        { value: 'horse', schema: undefined }
http://localhost:3000/horse.schema { value: 'horse', schema: 'schema' }

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

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