node.js - nodejs rest url中的模式匹配
问题描述
在我的节点应用程序中,我使用 router.use 进行令牌验证。我想跳过几个 url 的验证,所以我想检查 url 是否匹配,然后调用 next();
但是我想跳过的 URL 有一个 URLparam
例如,这是 URL /service/:appname/getall。 这必须与/service/blah/getall匹配并给出一个真实的。
如何在不使用“/”拆分 url 的情况下实现这一点
提前致谢。
解决方案
参数将匹配:[^/]+
,因为它后面是 a 1 次或多次:
以外的任何内容。/
如果您在模板中找到参数并将它们替换为匹配任何字符串的正则表达式,您可以按照您的要求进行操作。
let template = '/service/:appname/getall'
let url = '/service/blah/getall'
// find params and replace them with regex
template = template.replace(/:[^/]+/g, '([^/]+)')
// the template is now a regex string '/service/[^/]+/getall'
// which is essentially '/service/ ANYTHING THAT'S NOT A '/' /getall'
// convert to regex and only match from start to end
template = new RegExp(`^${template}$`)
// ^ = beggin
// $ = end
// the template is now /^\/service\/([^\/]+)\/getall$/
matches = url.match(template)
// matches will be null is there is no match.
console.log(matches)
// ["/service/blah/getall", "blah"]
// it will be [full_match, param1, param2...]
编辑:使用\w
而不是[^/]
,因为:
路由参数的名称必须由“单词字符”([A-Za-z0-9_])组成。https://expressjs.com/en/guide/routing.html#route-parameters
我相信大多数解析器都是如此,所以我更新了我的答案。以下测试数据仅适用于此更新的方法。
let template = '/service/:app-:version/get/:amt';
let url = '/service/blah-v1.0.0/get/all';
template = template.replace(/:\w+/g, `([^/]+)` );
template = new RegExp(`^${template}$`);
let matches = url.match(template);
console.log(url);
console.log(template);
console.log(matches);
// Array(4) ["/service/blah-v1.0.0/get/all", "blah", "v1.0.0", "all"]
推荐阅读
- c# - aspnetcore 从 1.0 升级到 2.1 后的 AWS Cognito 签名不匹配问题
- powershell - 如何从批处理文件自动运行 Exchange Powershell 命令
- ssis - 我需要使用 SSIS VB 脚本任务将扩展名为“.TXT”的文件夹中所有文件的扩展名更改为“.DAT”
- python-3.x - 使用 Flask-ReCaptcha 时出现 uWSGI 分段错误
- apache-kafka - 有没有办法确定 Kafka 主题中的消息来自哪里?
- .net - 如果我必须同时针对 Dot Net Core 3.0 和 .Net Framework 4.7.2,我应该在库中使用哪个 .Net 标准版本
- python - 如何从给定的 json 文件生成自己的 json 解析器?
- javascript - 如何使用 jQuery 选择动态值
- shopware - 从 CLI 设置 Shopware 5 商店的维护模式?
- css - 禁用离子按钮的样式在离子V4中不起作用