公司动态
micro-router 进阶路径匹配:用 UrlPattern 实例与正则表达式定制 URL 路由的隐藏技巧
micro-router 进阶路径匹配用 UrlPattern 实例与正则表达式定制 URL 路由的隐藏技巧【免费下载链接】micro-router:station: A tiny and functional router for Zeits Micro项目地址: https://gitcode.com/gh_mirrors/mi/micro-routermicro-router 是一款为 Zeit 的 Micro 框架打造的轻量级函数式路由库只需几行代码即可定义 RESTful 路由。本文聚焦它最实用的进阶玩法——路径匹配教你如何用 UrlPattern 实例和正则表达式定制 URL 路由轻松搞定:param通配符搞不定的复杂场景。 为什么需要进阶路径匹配默认情况下micro-router 的路径参数使用:记号例如/hello/:who会把who提取为参数。这很简单但遇到以下场景就显得力不从心要匹配所有以/api开头的请求路径中包含特殊字符如带的用户名想用正则表达式精确控制哪些 URL 能命中路由这时micro-router 就藏着一个隐藏技巧你可以直接传一个UrlPattern 实例作为路径参数。 快速上手安装 micro-router 与基础路由先安装依赖yarn add microrouter然后定义一个最简单的服务const { send } require(micro) const { router, get } require(microrouter) const hello (req, res) send(res, 200, Hello ${req.params.who}) module.exports router(get(/hello/:who, hello))请求/hello/World时req.params就会得到{ who: World }。基础部分到这里就完了下面进入正题。 核心技巧用 UrlPattern 实例作为路由路径micro-router 底层的路径匹配由 url-pattern 包完成。它允许你构造一个UrlPattern实例直接传给get()、post()等路由方法。官方文档readme.md中给出的示例非常直观const UrlPattern require(url-pattern) const { router, get } require(microrouter) const routes router( get( new UrlPattern(/^\api/), () This will match all routes that start with api ) )new UrlPattern(/^\api/)就是一个正则表达式包装出来的模式凡是 URL 以api开头的请求都会被这个路由捕获。当 path 参数是 UrlPattern 实例时micro-router 会跳过常规的字符串拼装直接用它去匹配请求路径——这就是隐藏技巧的关键。源码里的判断逻辑在 src/lib/index.js 中每条路由注册时都会判断const route isPattern(path) ? path : new UrlPattern(${namespace}${path}, patternOpts)如果path是 UrlPattern 实例就原样使用不做任何加工否则把命名空间前缀和路径字符串拼接起来用默认配置新建一个实例isPattern的定义在 src/utils/index.jsconst isPattern pattern pattern instanceof UrlPattern 正则表达式定制 URL 路由的 3 个实用技巧技巧 1用^锚定前缀一网打尽同类请求new UrlPattern(/^\api/)可以拦截所有/api/*请求非常适合做接口网关的统一日志、鉴权兜底或旧接口迁移。技巧 2用精确正则拦截特定路径测试用例 src/lib/index.test.js 中给出了精确匹配的写法const pattern new UrlPattern(/^\/api$/) const routes router(get(pattern, () with custom pattern))^\/api$只匹配恰好是/api的 URL多一个字符都不行。配合$锚点你可以做到只命中这一个地址的精确路由。技巧 3路由顺序决定命中结果micro-router 按注册顺序遍历路由第一个命中的处理函数生效。比如同时注册/path和/:param时请求/path只会进精确路由不会落到参数路由——这一点在 src/lib/index.test.js 的 multiple matching routes 用例中有明确验证。所以把精确路由放前面、正则兜底路由放后面是最稳妥的写法。⚠️ 容易踩的坑与隐藏细节坑 1UrlPattern 实例不兼容命名空间文档中特别提示如果你传的是 UrlPattern 实例withNamespace命名空间会失效。因为源码在识别到实例时跳过了${namespace}${path}的拼接步骤。想同时使用命名空间和正则路径要么自己把前缀写进正则里要么老老实实用字符串路径。细节 2参数字符集是隐藏配置readme.md 说参数用:记号但没告诉你参数允许哪些字符。答案在 src/utils/index.js 中const patternOpts { segmentNameCharset: a-zA-Z0-9_-, segmentValueCharset: a-zA-Z0-9.-_, }也就是说路由值里默认允许、.、、-等特殊字符用户名、版本号这类场景正好用得上而参数名则支持字母、数字、下划线和连字符。想全局改字符集只需在 src/lib/index.js 中替换传入的patternOpts。细节 3query 参数由谁解析无论路径是字符串还是 UrlPattern 实例查询串解析都走同一套逻辑getParamsAndQuery见 src/utils/index.js它用 Node 内置的url.parse(url, true)拆出query再用模式匹配出params最终挂到req.params和req.query上——这也是 src/utils/index.test.js 重点覆盖的两个场景。 小结需求推荐写法普通动态参数字符串路径/hello/:who匹配整批前缀请求new UrlPattern(/^\api/)精确拦截单一 URLnew UrlPattern(/^\/api$/)版本化分组路由withNamespace(/api/v1) 字符串路径micro-router 把路由匹配这件小事做到了极致几行代码注册路由需要精细控制时又能无缝切换到 UrlPattern 正则表达式。掌握 UrlPattern 实例这一个隐藏技巧你的 URL 路由能力就有了质的飞跃。【免费下载链接】micro-router:station: A tiny and functional router for Zeits Micro项目地址: https://gitcode.com/gh_mirrors/mi/micro-router创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考