修复路径中存在特殊符号的问题

This commit is contained in:
2025-04-27 14:10:21 +08:00
parent fd364fb393
commit f494ca4652
3 changed files with 11 additions and 6 deletions

View File

@ -128,6 +128,8 @@ export class KoaRouter {
const res = pathname.matchAll(/\{([a-zA-Z][a-zA-Z0-9_]*)(:([a-zA-Z][a-zA-Z0-9_]*))?\}/g);
const parsers: IKoaRouteConfig["parsers"] = {};
const resolveRegexp = (str: string) => str.replace(/\\/g, "\\\\").replace(/\./g, "\\.")
//处理参数,并生成正则表达式
const regexpItems: string[] = [];
let offset = 0;
@ -135,7 +137,7 @@ export class KoaRouter {
const name = match[1];
const type = match[3];
//放入路由前部分
regexpItems.push(pathname.slice(offset, match.index));
regexpItems.push(resolveRegexp(pathname.slice(offset, match.index)));
//放入路由参数部分
if (type) {
@ -150,7 +152,7 @@ export class KoaRouter {
}
//基础正则表达式
let regexpStr = "^" + regexpItems.join("") + pathname.slice(offset);
let regexpStr = "^" + regexpItems.join("") + resolveRegexp(pathname.slice(offset));
//严格模式
if (option?.strict ?? true) regexpStr += "$";