Browse Source

Change router to limit rule

HonorLee 5 years ago
parent
commit
8b50106296
2 changed files with 21 additions and 40 deletions
  1. 7 7
      system/core.js
  2. 14 33
      system/lib/core/router.js

+ 7 - 7
system/core.js

@@ -146,13 +146,13 @@ if(Config && Config.Wechat.on){
     }
 }
 
-let HandlerFiles = FILE.walkSync(Core.Path.Handler, {nodir: true});
-CACHE.Handlers = [];
-for(let i in HandlerFiles){
-    let path = HandlerFiles[i]['path'];
-    CACHE.Handlers.push(path.replace(Core.Path.Handler,''));
-}
-CACHE.HandlersString = CACHE.Handlers.join('|');
+// let HandlerFiles = FILE.walkSync(Core.Path.Handler, {nodir: true});
+// CACHE.Handlers = [];
+// for(let i in HandlerFiles){
+//     let path = HandlerFiles[i]['path'];
+//     CACHE.Handlers.push(path.replace(Core.Path.Handler,''));
+// }
+// CACHE.HandlersString = CACHE.Handlers.join('|');
 
 global.RouterRule = null;
 try{

+ 14 - 33
system/lib/core/router.js

@@ -19,45 +19,26 @@ var Router ={
         });
     },
     goHandler:function(path,req,res){
-        let pathArr = path.split('/');
         let method  = 'index';
-        let match   = false;
-        if(path=='/') path = '/index';
-        let expArr = [],methodMark = {};
         let handlerFile;
-        //
-        handlerFile = path + '/index.js';
-        expArr.push(handlerFile);
-        methodMark[handlerFile] = 'index';
-        //
-        handlerFile = path + '.js';
-        expArr.push(handlerFile);
-        methodMark[handlerFile] = 'index';
-        //
-        if(path!='/index'){
-            if(pathArr.length==2){
-                method = pathArr[1];
-                handlerFile = '/index.js';
-                expArr.push(handlerFile);
-                methodMark[handlerFile] = method;
+        if(path=='/'){
+            handlerFile = '/index.js'
+        }else if(path.lastIndexOf('/')==(path.length-1)){
+            handlerFile = `${path}index.js`;
+        }else{
+            if(FILE.existsSync(Core.Path.Handler + `${path}.js`)){
+                handlerFile = `${path}.js`;
             }else{
+                let pathArr = path.split('/');
                 method = pathArr.pop();
-                handlerFile = pathArr.join('/') + '/index.js';
-                expArr.push(handlerFile);
-                methodMark[handlerFile] = method;
-                //
-                handlerFile = pathArr.join('/') + '.js';
-                expArr.push(handlerFile);
-                methodMark[handlerFile] = method;
+                path = pathArr.join('/');
+                if(FILE.existsSync(Core.Path.Handler + `${path}.js`)){
+                    handlerFile = `${path}.js`;
+                }
             }
         }
-
-        match = new RegExp(expArr.join('|')).exec(CACHE.HandlersString);
-        if(!match) return Router._error('No such handler ['+handlerFile+']',res);
-
-        method = methodMark[match];
-        handlerFile = Core.Path.Handler + match;
-        if(method=='') method = 'index';
+        if(!handlerFile) return Router._error('No such handler ['+path+']',res);
+        handlerFile = Core.Path.Handler + handlerFile;
         if(method=='__construct') return Router._error('__construct can\'t be called outside in ['+handlerFile+']',res);
         let handler;
         try {