router.js 3.5 KB

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