router.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * @Author: HonorLee
  3. * @Version 2.0
  4. * @LastUpdate 2019/10/11
  5. */
  6. import PATH = require('path');
  7. export default class Router{
  8. public static async run(requestData:RequestData){
  9. let method = 'index';
  10. let handlerFile;
  11. Object.keys(SYSTEM.MVC.ROUTER_MAP).forEach(path=>{
  12. if(requestData.path && requestData.path.indexOf(path) == 0) requestData.path = `${SYSTEM.MVC.ROUTER_MAP[path]}${requestData.path.replace(path,'')}`;
  13. })
  14. if(requestData.path == '/'){
  15. handlerFile = '/index.js'
  16. }else if(requestData.path && requestData.path.match(/\/$/g)){
  17. handlerFile = `${requestData.path}index.js`;
  18. }else if(requestData.path){
  19. try{
  20. FILE.statSync(`${SYSTEM.MVC.PATH.CONTROLLER}${requestData.path}.js`)
  21. handlerFile = `${requestData.path}.js`;
  22. }catch(e){
  23. const pathArr = requestData.path.split('/');
  24. method = pathArr.pop() || 'index';
  25. const path = pathArr.join('/');
  26. try{
  27. FILE.statSync(`${SYSTEM.MVC.PATH.CONTROLLER}${path}.js`)
  28. handlerFile = `${path}.js`;
  29. }catch(e){
  30. try{
  31. FILE.statSync(`${SYSTEM.MVC.PATH.CONTROLLER}${path}/index.js`);
  32. handlerFile = `${path}/index.js`;
  33. }catch(e){
  34. handlerFile = null;
  35. }
  36. }
  37. }
  38. }else{
  39. handlerFile = null;
  40. }
  41. if(method == '__construct') return Router._error(403,'Function __construct can\'t be called outside',requestData.res);
  42. if(!handlerFile) return Router._error(404,`No such handler [${requestData.path}]`,requestData.res);
  43. handlerFile = `${SYSTEM.MVC.PATH.CONTROLLER}${handlerFile}`;
  44. let handlerClass,handler;
  45. try {
  46. // eslint-disable-next-line @typescript-eslint/no-var-requires
  47. handlerClass = require(handlerFile);
  48. if(handlerClass.default) handlerClass = handlerClass.default;
  49. }catch(e:any){
  50. console.log(e.stack);
  51. if(e.code=='MODULE_NOT_FOUND'){
  52. Router._error(404,null,requestData.res);
  53. }else{
  54. Router._error(404,e.stack,requestData.res);
  55. }
  56. return;
  57. }
  58. try{
  59. handler = new handlerClass(requestData);
  60. // const mixHandler = handler;
  61. }catch(e:any){
  62. return Router._error(403,e.stack,requestData.res);
  63. }
  64. // if(handler instanceof CONTROLLER){
  65. // mixHandler = handler;
  66. // }else{
  67. // const baseClass = new CONTROLLER(requestData);
  68. // mixHandler = Object.assign(baseClass,handler);
  69. // }
  70. if(typeof handler[method]==='function'){
  71. let returnSometing;
  72. if(typeof handler['__construct']==='function'){
  73. try{
  74. returnSometing = await handler['__construct'].call(handler);
  75. }catch(e:any){
  76. LOGGER.error(`Function __construct in [${handlerFile}] called error:`);
  77. Router._error(403,e.stack,requestData.res);
  78. return;
  79. }
  80. }
  81. try{
  82. handler[method].call(handler,returnSometing);
  83. }catch(e:any){
  84. LOGGER.error(`Function [${method}] in [${handlerFile}] called error:`);
  85. Router._error(403,e.stack,requestData.res);
  86. }
  87. }else{
  88. Router._error(404,`No such method [${method}] in handler [${handlerFile}]`,requestData.res);
  89. }
  90. }
  91. private static _error(code:number,errMsg:string|null,res:any){
  92. if(errMsg) LOGGER.error(errMsg);
  93. res.writeHead(code, {'Content-Type': 'text/html'});
  94. res.end(`<center style="font-size:24px">- ${code} -</center>`);
  95. }
  96. // private static _encodeRequestData(req){
  97. // req._GET = Router._encodeObj(req._GET);
  98. // req._POST = Router._encodeObj(req._POST);
  99. // }
  100. // private static _encodeObj(obj){
  101. // let newObj = {};
  102. // Object.keys(obj).forEach(key=>{
  103. // if(typeof(obj[key]==='string')){
  104. // newObj[encodeURI(key)] = encodeURI(obj[key]);
  105. // }else{
  106. // newObj[encodeURI(key)] = obj[key];
  107. // }
  108. // })
  109. // return newObj;
  110. // }
  111. }
  112. // module.exports = Router;