router.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @Author HonorLee (deve@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 handlerFile = Core.Path.Handler + path + '.js';
  21. let pathArr = path.split('/');
  22. let method = pathArr.pop();
  23. if(!method) method = 'index';
  24. if(CACHE.router[handlerFile]){
  25. Router.runHandler(handlerFile,method,req,res);
  26. return;
  27. }
  28. FILE.stat(handlerFile,function(err,status){
  29. if(err || !status.isFile()){
  30. if(pathArr.length<=1) return Router._error('No such handler ['+handlerFile+']',res);
  31. handlerFile = Core.Path.Handler + pathArr.join('/') + '.js';
  32. FILE.stat(handlerFile,function(err,status){
  33. if(err || !status.isFile()){
  34. Router._error('No such handler ['+handlerFile+']',res);
  35. }else{
  36. Router.runHandler(handlerFile,method,req,res);
  37. }
  38. });
  39. }else{
  40. Router.runHandler(handlerFile,method,req,res);
  41. }
  42. });
  43. },
  44. runHandler:function(handlerFile,method,req,res){
  45. let handler;
  46. try {
  47. handler = require(handlerFile);
  48. }catch(e){
  49. Router._error(e.stack,res);
  50. }
  51. let newHandlerClass = Object.assign({
  52. 'Request' : req,
  53. 'Response' : res,
  54. 'COOKIE' : req._Cookie,
  55. 'GET' : req._GET,
  56. 'POST' : req._POST,
  57. 'UPLOAD' : req._UPLOAD
  58. },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=='/'?'/index':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. };