mvc.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //HTTP服务
  2. import HTTP = require('http');
  3. import URL = require('url');
  4. //表单处理器
  5. import formidable = require('formidable')
  6. //路由控制器
  7. import Router from './lib/router';
  8. //模型控制器
  9. import Module from './lib/module';
  10. //Session控制器
  11. import Session from './lib/session';
  12. //控制器基类
  13. import ControllerClass from './lib/controller';
  14. (global as any).CONTROLLER = ControllerClass;
  15. const IncomingForm = formidable.IncomingForm;
  16. class MVC {
  17. constructor(option:{root_path:string,server_port:number}) {
  18. //定义全局MVC框架变量
  19. const Root = `${SYSTEM.ROOTPATH}/${option.root_path}`;
  20. SYSTEM.MVC = {
  21. ROOT:Root,
  22. PORT:option.server_port,
  23. PATH:{
  24. MODULE:`${Root}/module`,
  25. CONTROLLER:`${Root}/controller`,
  26. VIEW:`${Root}/view`,
  27. },
  28. ROUTER_MAP:null,
  29. ROUTER:Router,
  30. SESSION:Session,
  31. EJS:require('ejs'),
  32. start:()=>{this.start()}
  33. };
  34. //初始化Module控制器
  35. Module.init();
  36. //初始化MVC应用目录
  37. Object.keys(SYSTEM.MVC.PATH).forEach((path)=>{
  38. try{
  39. FILE.statSync(SYSTEM.MVC.PATH[path]);
  40. }catch(e:any){
  41. if(e.code=='ENOENT'){
  42. FILE.mkdirsSync(SYSTEM.MVC.PATH[path]);
  43. }
  44. }
  45. });
  46. //加载路由Map文件或初始化
  47. const mapFile = `${SYSTEM.MVC.ROOT}/map.json`;
  48. try{
  49. FILE.statSync(mapFile);
  50. SYSTEM.MVC.ROUTER_MAP = JSON.parse(FILE.readFileSync(mapFile,'UTF-8'));
  51. }catch(e:any){
  52. if(e.code=='ENOENT'){
  53. FILE.writeFileSync(mapFile,'{}','UTF-8');
  54. }else{
  55. LOGGER.error(`MVC Router map file [${mapFile}] parse error!`);
  56. }
  57. SYSTEM.MVC.ROUTER_MAP = {};
  58. }
  59. }
  60. public start(){
  61. try{
  62. HTTP.createServer(this._serverHandler).listen(SYSTEM.MVC.PORT);
  63. LOGGER.info(`Http MVC Server start at port [${SYSTEM.MVC.PORT}]`);
  64. }catch(e:any){
  65. LOGGER.error(`Http MVC Server start failed at port [${SYSTEM.MVC.PORT}]`);
  66. LOGGER.error(e.stack);
  67. }
  68. }
  69. protected _serverHandler(req?:HTTP.IncomingMessage,res?:HTTP.ServerResponse){
  70. if(!req) return;
  71. const requestData:RequestData = {
  72. req,res,
  73. path:'' as string|null,
  74. refer:URL.parse(req.headers.referer||''),
  75. method:req.method?.toLowerCase()||'get',
  76. COOKIE:{} as any,
  77. GET:{},
  78. POST:{},
  79. UPLOAD:{},
  80. SESSION:{} as any
  81. }
  82. // if(requestData.path==URL.parse(req.headers.referer as string,true).pathname) return;
  83. req.headers.cookie && req.headers.cookie.split(';').forEach((Cookie)=>{
  84. const parts:Array<string> = Cookie.split('=');
  85. let key = parts.shift();
  86. if (key){
  87. key = key.trim();
  88. const value = parts.join('=').trim();
  89. requestData.COOKIE[key] = value;
  90. }
  91. });
  92. requestData.SESSION = new Session(requestData,requestData.COOKIE.SESSIONID);
  93. const URLParse = URL.parse(req.url as string,true);
  94. if(URLParse){
  95. requestData.path = URLParse.pathname,
  96. requestData.GET = URLParse.query;
  97. }
  98. if(req.method && req.method.toLowerCase()=='post'){
  99. new IncomingForm().parse(req, (err, fields, files)=>{
  100. requestData.POST = fields;
  101. requestData.UPLOAD = files;
  102. Router.run(requestData);
  103. });
  104. }else{
  105. Router.run(requestData);
  106. }
  107. }
  108. }
  109. module.exports = MVC;