[英]ExpressJs regex in routing
I am currently trying to create a calculator API using ExpressJs.我目前正在尝试使用 ExpressJs 创建一个计算器 API。 My problem is using regex as routes to determine common patterns.我的问题是使用正则表达式作为路由来确定常见模式。
A valid typical route is api/calculator/:operator/:number1/:number2
where by operator
is a mathematical operator in a string format (add, subtract, multiply, divide) and number1
and number2
are operands.一个有效的典型路径是api/calculator/:operator/:number1/:number2
其中 by operator
是字符串格式的数学运算符(加、减、乘、除),而number1
和number2
是操作数。
My aim is, if any number is missed in the route, eg api/calculator/:operator/:number1/
, or api/calculator/:operator//:number2
or api/calculator/:operator//
I want to send a response that the operands are invalid.我的目标是,如果路线中缺少任何数字,例如api/calculator/:operator/:number1/
或api/calculator/:operator//:number2
或api/calculator/:operator//
我想发送一个操作数无效的响应。
Also, if the operator is not valid, a response needs to be sent as a well.此外,如果运算符无效,则也需要发送响应。
How can I use regex to determine if the route is a valid one?如何使用正则表达式来确定路由是否有效?
In your routing you can use a regex to require at least one char in each of the three params:在您的路由中,您可以使用正则表达式来要求三个参数中的每个参数中至少有一个字符:
app.get('api/calculator/:operator(.{1,})/:number1(.{1,})/:number2(.{1,})', (req, res) => { ... });
app.get('api/calculator/*', (req, res) => { ... });
The first routing is used for valid patterns, the second routing is used for error reporting第一个路由用于有效模式,第二个路由用于错误报告
You can also apply a pattern to further restrict the routing, such as alpha only for the operator, and numbers only for the numbers:您还可以应用模式来进一步限制路由,例如仅用于操作员的 alpha 和仅用于数字的 numbers:
app.get('api/calculator/:operator([A-Za-z]+)/:number1([+-]?[0-9.]+)/:number2([+-]?[0-9.]+)', (req, res) => { ... });
Or, go for more fancy number validation with ([+-]?[0-9]+(\\.[0-9]+)?)
或者,go 使用([+-]?[0-9]+(\\.[0-9]+)?)
进行更花哨的数字验证
First Wow this is Just Wrong首先哇,这是错的
My aim is, if any number is missed in the route, eg
api/calculator/:operator/:number1/
, orapi/calculator/:operator//:number2
orapi/calculator/:operator//
I want to send a response that the operands are invalid.我的目标是,如果路线中缺少任何数字,例如api/calculator/:operator/:number1/
或api/calculator/:operator//:number2
或api/calculator/:operator//
我想发送一个操作数无效的响应。
If the route is defined you can't interchange them如果路线已定义,则不能互换它们
The thing with express routes is that its Relative in nature so if a GET
Route Such as this is called api/calculator/:operator/:number1/:number2
as api/calculator/*/3/3
快速路由的问题是它本质上是相对的,所以如果像这样的GET
路由被称为api/calculator/:operator/:number1/:number2
as api/calculator/*/3/3
the operator would be "+" and number1 would be "3" and number2 would be "3" but in a GET
Route api/calculator/*/3
or api/calculator/*
运算符为“+”,number1 为“3”,number2 为“3”,但在GET
Route api/calculator/*/3
或api/calculator/*
the operator would be "+" and number1 would be "3" and number2 would be "undefined"运算符为“+”,number1 为“3”,number2 为“undefined”
The correct way is to handle it as Express Query正确的方法是将其作为 Express Query 处理
So your code should be like api/calculator/?operator="+"&number1="3"&number2="3"
所以你的代码应该像api/calculator/?operator="+"&number1="3"&number2="3"
So this way you can access the query and the application won't crash when interchange using req.query.value
因此,您可以通过这种方式访问查询,并且在使用req.query.value
进行交换时应用程序不会崩溃
https://expressjs.com/en/api.html#req.query to read more https://expressjs.com/en/api.html#req.query阅读更多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.