From c813e9cb4f712f5f4e0d386d6ce6b5e81e8d6500 Mon Sep 17 00:00:00 2001 From: suwenxiong Date: Wed, 13 Feb 2019 14:56:05 +0800 Subject: [PATCH] =?UTF-8?q?opti:=20=E4=BC=98=E5=8C=96=20restful=20api=20?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=B7=AF=E7=94=B1=E5=8C=B9=E9=85=8D=E7=AE=97?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=9D=83=E9=87=8D=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/middleware/mockServer.js | 26 +++++++++++++++++++------- test/server/mockServer.test.js | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/server/middleware/mockServer.js b/server/middleware/mockServer.js index 4f9a24cc..37cf51c3 100755 --- a/server/middleware/mockServer.js +++ b/server/middleware/mockServer.js @@ -14,7 +14,10 @@ const variable = require('../../client/constants/variable.js') function matchApi(apiPath, apiRule) { let apiRules = apiRule.split('/'); let apiPaths = apiPath.split('/'); - let pathRules = {}; + let pathParams = { + __weight: 0 + }; + if (apiPaths.length !== apiRules.length) { return false; } @@ -29,9 +32,9 @@ function matchApi(apiPath, apiRule) { apiRules[i][0] === '{' && apiRules[i][apiRules[i].length - 1] === '}' ) { - pathRules[apiRules[i].substr(1, apiRules[i].length - 2)] = apiPaths[i]; + pathParams[apiRules[i].substr(1, apiRules[i].length - 2)] = apiPaths[i]; } else if (apiRules[i].indexOf(':') === 0) { - pathRules[apiRules[i].substr(1)] = apiPaths[i]; + pathParams[apiRules[i].substr(1)] = apiPaths[i]; } else if ( apiRules[i].length > 2 && apiRules[i].indexOf('{') > -1 && @@ -40,7 +43,7 @@ function matchApi(apiPath, apiRule) { let params = []; apiRules[i] = apiRules[i].replace(/\{(.+?)\}/g, function(src, match) { params.push(match); - return '(.+)'; + return '([^\\/\\s]+)'; }); apiRules[i] = new RegExp(apiRules[i]); if (!apiRules[i].test(apiPaths[i])) { @@ -50,15 +53,17 @@ function matchApi(apiPath, apiRule) { let matchs = apiPaths[i].match(apiRules[i]); params.forEach((item, index) => { - pathRules[item] = matchs[index + 1]; + pathParams[item] = matchs[index + 1]; }); } else { if (apiRules[i] !== apiPaths[i]) { return false; + }else{ + pathParams.__weight++; } } } - return pathRules; + return pathParams; } function parseCookie(str) { @@ -220,9 +225,16 @@ module.exports = async (ctx, next) => { //处理动态路由 if (!interfaceData || interfaceData.length === 0) { let newData = await interfaceInst.getVar(project._id, ctx.method); - let findInterface = _.find(newData, item => { + + let findInterface; + let weight = 0; + _.each(newData, item => { let m = matchApi(newpath, item.path); if (m !== false) { + if(m.__weight >= weight){ + findInterface = item; + } + delete m.__weight; ctx.request.query = Object.assign(m, ctx.request.query); return true; } diff --git a/test/server/mockServer.test.js b/test/server/mockServer.test.js index 2687a579..c2e87988 100644 --- a/test/server/mockServer.test.js +++ b/test/server/mockServer.test.js @@ -28,14 +28,25 @@ test('matchApi', t => { let r5 = matchApi('/user/ac/ttt/bd', apiRule_5); t.deepEqual(r5, { aaa: 'ac', - bbbb: 'bd' + bbbb: 'bd', + __weight: 2 }); const apiRule_6 = '/user/a1={aaa}/ttt/b1={bbbb}'; let r6 = matchApi('/user/a1=aaa/ttt/b1=111q', apiRule_6); t.deepEqual(r6, { aaa: 'aaa', - bbbb: '111q' + bbbb: '111q', + __weight: 2 + }); + + + const apiRule_7 = '/user/a1={aaa}/ttt/b1={bbbb}/xxx/yyy'; + let r7 = matchApi('/user/a1=aaa/ttt/b1=111q/xxx/yyy', apiRule_7); + t.deepEqual(r7, { + aaa: 'aaa', + bbbb: '111q', + __weight: 4 }); });