expressjs是否可以通过编程方式测试网址格式是否匹配?
我想对expressjs模式执行类似的操作
isMatch([expressjs_pattern], [path_string])
EX:
isMatch('/users/:userId/roles/:roleCode/$', '/users/123/roles/admin/')
// result = true
是否愿意这样做?
回答如下:按照Express的路由指南,在路由路径(here):]中]
Express使用path-to-regexp来匹配路由路径;有关定义路由路径的所有可能性,请参阅正则表达式路径文档。
因此您可以
npm install path-to-regexp
并根据模式测试字符串:
const p2r = require('path-to-regexp');
const regexp = p2r('/users/:userId/roles/:roleCode');
regexp.test('/users/123/roles/admin'); // true
expressjs是否可以通过编程方式测试网址格式是否匹配?
我想对expressjs模式执行类似的操作
isMatch([expressjs_pattern], [path_string])
EX:
isMatch('/users/:userId/roles/:roleCode/$', '/users/123/roles/admin/')
// result = true
是否愿意这样做?
回答如下:按照Express的路由指南,在路由路径(here):]中]
Express使用path-to-regexp来匹配路由路径;有关定义路由路径的所有可能性,请参阅正则表达式路径文档。
因此您可以
npm install path-to-regexp
并根据模式测试字符串:
const p2r = require('path-to-regexp');
const regexp = p2r('/users/:userId/roles/:roleCode');
regexp.test('/users/123/roles/admin'); // true