|
@@ -1,4 +1,6 @@
|
|
|
'use strict'
|
|
|
+CACHE.router = {};
|
|
|
+
|
|
|
var Router ={
|
|
|
goAsset:function(path,req,res){
|
|
|
let assetFile = Core.Path.Asset+path;
|
|
@@ -12,41 +14,57 @@ var Router ={
|
|
|
},
|
|
|
goHandler:function(path,req,res){
|
|
|
let handlerFile = Core.Path.Handler + path + '.js';
|
|
|
- let method = 'index';
|
|
|
let pathArr = path.split('/');
|
|
|
- let Res = res;
|
|
|
- let Req = req;
|
|
|
+ let method = pathArr.pop();
|
|
|
+
|
|
|
+ if(!method) method = 'index';
|
|
|
|
|
|
+ if(CACHE.router[handlerFile]){
|
|
|
+ Router.runHandler(handlerFile,method,req,res);
|
|
|
+ return;
|
|
|
+ }
|
|
|
FILE.stat(handlerFile,function(err,status){
|
|
|
if(err || !status.isFile()){
|
|
|
- method = pathArr.pop();
|
|
|
- if(pathArr.length<=1) return Router._error('No such handler ['+handlerFile+']',Res);
|
|
|
+ if(pathArr.length<=1) return Router._error('No such handler ['+handlerFile+']',res);
|
|
|
handlerFile = Core.Path.Handler + pathArr.join('/') + '.js';
|
|
|
FILE.stat(handlerFile,function(err,status){
|
|
|
if(err || !status.isFile()){
|
|
|
- Router._error('No such handler ['+handlerFile+']',Res);
|
|
|
+ Router._error('No such handler ['+handlerFile+']',res);
|
|
|
}else{
|
|
|
- Router.runHandler(handlerFile,method,Req,Res);
|
|
|
+ Router.runHandler(handlerFile,method,req,res);
|
|
|
}
|
|
|
});
|
|
|
}else{
|
|
|
- Router.runHandler(handlerFile,method,Req,Res);
|
|
|
+ Router.runHandler(handlerFile,method,req,res);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
runHandler:function(handlerFile,method,req,res){
|
|
|
+ let handler;
|
|
|
try {
|
|
|
- let handler = require(handlerFile);
|
|
|
- if(typeof handler[method]==='function'){
|
|
|
- let noBypass = true;
|
|
|
- if(typeof handler['__construct']==='function') noBypass = handler['__construct'](req,res);
|
|
|
- if(noBypass || noBypass===undefined) handler[method](req,res);
|
|
|
- }else{
|
|
|
- Router._error('Handler ['+handlerFile+'] no such method "'+method+'"',res);
|
|
|
- }
|
|
|
+ handler = require(handlerFile);
|
|
|
}catch(e){
|
|
|
Router._error(e.stack,res);
|
|
|
}
|
|
|
+
|
|
|
+ let newHandlerClass = Object.assign({
|
|
|
+ 'Request' : req,
|
|
|
+ 'Response' : res,
|
|
|
+ 'COOKIE' : req._Cookie,
|
|
|
+ 'GET' : req._GET,
|
|
|
+ 'POST' : req._POST,
|
|
|
+ 'UPLOAD' : req._UPLOAD
|
|
|
+ },handler);
|
|
|
+
|
|
|
+ if(!CACHE.router[handlerFile]){ CACHE.router[handlerFile] = true;}
|
|
|
+
|
|
|
+ if(newHandlerClass.hasOwnProperty(method) && typeof newHandlerClass[method]==='function'){
|
|
|
+ let noBypass = true;
|
|
|
+ if(newHandlerClass.hasOwnProperty('__construct') && typeof newHandlerClass['__construct']==='function') noBypass = newHandlerClass['__construct']();
|
|
|
+ if(noBypass || noBypass===undefined) newHandlerClass[method]();
|
|
|
+ }else{
|
|
|
+ Router._error('Handler ['+handlerFile+'] no such method "'+method+'"',res);
|
|
|
+ }
|
|
|
},
|
|
|
_error:function(log,res){
|
|
|
LOGGER.error(log);
|
|
@@ -60,11 +78,11 @@ module.exports = function(req,res){
|
|
|
let URLParse = URL.parse(URI,true);
|
|
|
let URLArr = URLParse.pathname.split('/');
|
|
|
let enterURI = String(URLArr[1])==''?'index':String(URLArr[1]);
|
|
|
- let isAsset = enterURI == Config.asset_path;
|
|
|
+ let isAsset = enterURI == Config.Asset.asset_path;
|
|
|
|
|
|
req._GET = URLParse.query;
|
|
|
if(isAsset){
|
|
|
- let assetPath = URLArr.join('/').replace('/'+Config.asset_path,'');
|
|
|
+ let assetPath = URLArr.join('/').replace('/'+Config.Asset.asset_path,'');
|
|
|
Router.goAsset(assetPath,req,res);
|
|
|
return;
|
|
|
}
|