router.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. 'use strict'
  7. CACHE.router = {};
  8. var Router ={
  9. goAsset:function(path,req,res){
  10. let assetFile = Core.Path.Asset+path;
  11. FILE.stat(assetFile,function(err,status){
  12. if(err || !status.isFile()){
  13. Router._error('No such file ['+path+']',res);
  14. }else{
  15. STATIC.load(assetFile,req,res);
  16. }
  17. });
  18. },
  19. goHandler:function(path,req,res){
  20. let pathArr = path.split('/');
  21. let method = 'index';
  22. let match = false;
  23. if(path=='/') path = '/index';
  24. let expArr = [],methodMark = {};
  25. let handlerFile;
  26. //
  27. handlerFile = path + '/index.js';
  28. expArr.push(handlerFile);
  29. methodMark[handlerFile] = 'index';
  30. //
  31. handlerFile = path + '.js';
  32. expArr.push(handlerFile);
  33. methodMark[handlerFile] = 'index';
  34. //
  35. if(path!='/index'){
  36. method = pathArr.pop();
  37. handlerFile = pathArr.join('/') + '/index.js';
  38. expArr.push(handlerFile);
  39. methodMark[handlerFile] = method;
  40. //
  41. handlerFile = pathArr.join('/') + '.js';
  42. expArr.push(handlerFile);
  43. methodMark[handlerFile] = method;
  44. }
  45. match = new RegExp(expArr.join('|')).exec(CACHE.HandlersString);
  46. if(!match) return Router._error('No such handler ['+handlerFile+']',res);
  47. method = methodMark[match];
  48. handlerFile = Core.Path.Handler + match;
  49. let handler;
  50. try {
  51. handler = require(handlerFile);
  52. }catch(e){
  53. Router._error(e.stack,res);
  54. }
  55. let baseClass = new HANDLER(req,res);
  56. let newHandlerClass = Object.assign(baseClass,handler);
  57. if(!CACHE.router[handlerFile]){ CACHE.router[handlerFile] = true;}
  58. if(newHandlerClass.hasOwnProperty(method) && typeof newHandlerClass[method]==='function'){
  59. let noBypass = true;
  60. if(newHandlerClass.hasOwnProperty('__construct') && typeof newHandlerClass['__construct']==='function') noBypass = newHandlerClass['__construct']();
  61. if(noBypass || noBypass===undefined) newHandlerClass[method]();
  62. }else{
  63. Router._error('Handler ['+handlerFile+'] no such method "'+method+'"',res);
  64. }
  65. },
  66. _error:function(log,res){
  67. LOGGER.error(log);
  68. res.writeHead(404, {'Content-Type': 'text/html'});
  69. res.end('404');
  70. }
  71. };
  72. module.exports = function(req,res){
  73. let URI = req.url;
  74. let URLParse = URL.parse(URI,true);
  75. let URLArr = URLParse.pathname.split('/');
  76. let enterURI = String(URLArr[1])==''?'index':String(URLArr[1]);
  77. let isAsset = enterURI == Config.Asset.asset_path;
  78. req._GET = URLParse.query;
  79. if(isAsset){
  80. let assetPath = URLArr.join('/').replace('/'+Config.Asset.asset_path,'');
  81. Router.goAsset(assetPath,req,res);
  82. return;
  83. }
  84. req._Cookie = {};
  85. req.headers.cookie && req.headers.cookie.split(';').forEach(function(Cookie){
  86. let parts = Cookie.split('=');
  87. let key = parts[0].trim();
  88. let value = (parts[1]||'').trim();
  89. if(parts.length>2){
  90. parts = parts.shift();
  91. value = parts.join('=').trim();
  92. }
  93. req._Cookie[key] = value;
  94. });
  95. Router.goHandler(URLParse.pathname,req,res);
  96. return this;
  97. };