diff --git a/package.json b/package.json index 03cdbd2..531d530 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,12 @@ { "name": "@yizhi/koa-router", - "version": "1.0.3", + "version": "1.0.4", "main": "dist/index.js", "types": "typing/index.d.ts", - "scripts": {}, + "scripts": { + "watch": "rm -rf dist typing tsconfig.tsbuildinfo && tsc -w --inlineSourceMap", + "build": "rm -rf dist typing tsconfig.tsbuildinfo && tsc" + }, "author": "", "license": "ISC", "description": "", @@ -13,4 +16,4 @@ "devDependencies": { "@types/koa": "^2.15.0" } -} +} \ No newline at end of file diff --git a/src/router.ts b/src/router.ts index 042507c..184f9ce 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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 += "$"; diff --git a/tsconfig.json b/tsconfig.json index 95f061e..86ff915 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -48,7 +48,7 @@ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ - "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ "outDir": "./dist", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */