controller.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. export default class Controller{
  7. private _requestData:RequestData;
  8. private _Request:any;
  9. private _Response:any;
  10. public COOKIE:any;
  11. public GET:any;
  12. public POST:any;
  13. public UPLOAD:any;
  14. constructor(requestData:RequestData) {
  15. this._requestData = requestData;
  16. this._Request = requestData.req;
  17. this._Response = requestData.res;
  18. this.COOKIE = requestData.COOKIE;
  19. this.GET = requestData.GET;
  20. this.POST = requestData.POST;
  21. this.UPLOAD = requestData.UPLOAD;
  22. if(this.GET._time) delete this.GET._time;
  23. if(this.POST._time) delete this.POST._time;
  24. // console.log(this._constructor)
  25. }
  26. /**
  27. * [response description]
  28. * @return {[type]} [description]
  29. */
  30. end(data:any,status?:number,mime?:string):void{
  31. let endContent = data,statusCode = 200,mimeType = 'text/plain';
  32. if(!endContent) endContent = '';
  33. if(typeof endContent == 'number') endContent = `${endContent}`;
  34. if(status) statusCode = status;
  35. if(mime) mimeType = mime;
  36. if(status) this._Response.statusCode = status;
  37. if(mimeType) this._Response.setHeader('Content-Type', `${mimeType}; charset=UTF-8`);
  38. try{
  39. this._Response.write(endContent);
  40. this._Response.end();
  41. }catch(e:any){
  42. LOGGER.error(e.stack)
  43. }
  44. }
  45. endRedirect(location:string,permanently?:boolean){
  46. const status = permanently?301:302;
  47. this._Response.writeHead(status,{'Location':location});
  48. this._Response.end();
  49. }
  50. /**
  51. * [responseInJSON description]
  52. * @param {[type]} somthing [String,Number,Object]
  53. */
  54. endJSON(somthing:any){
  55. let endContent;
  56. if(typeof somthing == 'string' || typeof somthing == 'number'){
  57. endContent = {data:somthing};
  58. }else if(typeof somthing == 'object'){
  59. endContent = somthing;
  60. }else{
  61. return this._error(new Error('Function endInJSON argument type must be string,number or object'));
  62. }
  63. this.end(JSON.stringify(endContent),200,'text/json');
  64. }
  65. /**
  66. * [responseAPI description]
  67. * @param {[type]} errorCode [description]
  68. * @param {[type]} somthing [description]
  69. */
  70. endAPI(errorCode:number,somthing:any){
  71. const endContent = {error:errorCode,data:null};
  72. if(typeof somthing == 'string' || typeof somthing == 'number' || typeof somthing == 'object'){
  73. endContent.data = somthing;
  74. this.end(JSON.stringify(endContent),200,'text/json');
  75. }else{
  76. this._error(new Error('Function endAPI argument type must be string,number or object'));
  77. }
  78. }
  79. /**
  80. * [responseAPI description]
  81. * @param {[type]} errorCode [description]
  82. * @param {[type]} somthing [description]
  83. */
  84. endView(viewname:string,data:any,option?:any){
  85. const viewFilePath = `${SYSTEM.MVC.PATH.VIEW}/${viewname}`;
  86. let tpl;
  87. try{
  88. tpl = FILE.readFileSync(`${viewFilePath}.html`,'utf-8');
  89. }catch(e){
  90. try{
  91. tpl = FILE.readFileSync(`${viewFilePath}.tpl`,'utf-8');
  92. }catch(e){
  93. }
  94. }
  95. if(!tpl) return this.end(`View template [${viewname}] not exist!`);
  96. const render = SYSTEM.MVC.EJS.compile(tpl,option);
  97. const renderedTpl = render(data);
  98. this.end(200,renderedTpl,'text/html');
  99. }
  100. /**
  101. * [responseView description]
  102. * @param {[type]} viewName [description]
  103. * @param {[type]} data [description]
  104. * @return {[type]} [description]
  105. */
  106. // endView(viewName,data){
  107. // let view = new VIEW(viewName,data);
  108. // if(view && view.html){
  109. // this.end(200,view.html,'text/html');
  110. // }else{
  111. // this.end(502,`View [${viewName}] not found`,'text/plain');
  112. // }
  113. // }
  114. setCookie(key:string,value:any,expireHour?:number|'forever',path?:string){
  115. let cookie,originCookie;
  116. if(this._Response.hasHeader('Set-Cookie')){
  117. originCookie = this._Response.getHeader('Set-Cookie');
  118. }
  119. if(!originCookie) originCookie = [];
  120. if(typeof originCookie == 'string') originCookie = [originCookie];
  121. cookie = `${key}=${value}`;
  122. if(path) cookie += ';path=' + path;
  123. if(expireHour){
  124. let expireTime;
  125. if(expireHour=='forever'){
  126. expireTime = new Date('9999/01/01 00:00:00').toUTCString();
  127. }else{
  128. expireTime = new Date(new Date().getTime()+Number(expireHour)*1000*60).toUTCString();
  129. }
  130. cookie += ';expires=' + expireTime;
  131. }
  132. originCookie.push(cookie);
  133. this._Response.setHeader('Set-Cookie',cookie);
  134. }
  135. renewCookie(key:string,expire:number|'forever',path?:string){
  136. const originCookieValue = this.COOKIE[key];
  137. if(!originCookieValue) return false;
  138. this.setCookie(key,originCookieValue,expire,path);
  139. }
  140. _error(err:any){
  141. LOGGER.error(err.stack);
  142. this.end(`<center style="font-size:24px">- 403 -</center>`,403,'text/html');
  143. }
  144. }