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

时间:2015-07-30 14:43:37

标签: javascript regex node.js express routes

我想在我的网址中获取一个值。

我的网址就像:

  

主机/:value.schema

我想获得价值。

例如:

  

host / horse.schema value = horse

我有另一条没有.schema的路线:

  

主机/:值

例如:

  

主人/马值=马

如何告诉Express有所作为?

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

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

你会收到这个:

http://localhost:3000/horse        { value: 'horse', schema: undefined }
http://localhost:3000/horse.schema { value: 'horse', schema: 'schema' }
相关问题