router.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if(method=='__construct') return Router._error('__construct can\'t be called outside in ['+handlerFile+']',res);
  50. let handler;
  51. try {
  52. handler = require(handlerFile);
  53. }catch(e){
  54. Router._error(e.stack,res);
  55. }
  56. if(typeof handler === 'function') handler = new handler();
  57. let baseClass = new HANDLER(req,res);
  58. let newHandlerClass = Object.assign(baseClass,handler);
  59. if(!CACHE.router[handlerFile]){ CACHE.router[handlerFile] = true;}
  60. if(newHandlerClass.hasOwnProperty(method) && typeof newHandlerClass[method]==='function'){
  61. let noBypass = true;
  62. if(newHandlerClass.hasOwnProperty('__construct') && typeof newHandlerClass['__construct']==='function') noBypass = newHandlerClass['__construct']();
  63. if(noBypass || noBypass===undefined) newHandlerClass[method]();
  64. }else{
  65. Router._error('Handler ['+handlerFile+'] no such method "'+method+'"',res);
  66. }
  67. },
  68. _error:function(log,res){
  69. LOGGER.error(log);
  70. res.writeHead(404, {'Content-Type': 'text/html'});
  71. res.end('404');
  72. }
  73. };
  74. module.exports = function(req,res){
  75. let URI = req.url;
  76. let URLParse = URL.parse(URI,true);
  77. let URLArr = URLParse.pathname.split('/');
  78. let enterURI = String(URLArr[1])==''?'index':String(URLArr[1]);
  79. let isAsset = enterURI == Config.Asset.asset_path;
  80. req._GET = URLParse.query;
  81. if(isAsset){
  82. let assetPath = URLArr.join('/').replace('/'+Config.Asset.asset_path,'');
  83. Router.goAsset(assetPath,req,res);
  84. return;
  85. }
  86. req._Cookie = {};
  87. req.headers.cookie && req.headers.cookie.split(';').forEach(function(Cookie){
  88. let parts = Cookie.split('=');
  89. let key = parts[0].trim();
  90. let value = (parts[1]||'').trim();
  91. if(parts.length>2){
  92. parts = parts.shift();
  93. value = parts.join('=').trim();
  94. }
  95. req._Cookie[key] = value;
  96. });
  97. Router.goHandler(URLParse.pathname,req,res);
  98. return this;
  99. };