handler.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. 'use strict'
  7. module.exports = function(req,res){
  8. this.Request = req;
  9. this.Response = res;
  10. this.COOKIE = req._Cookie;
  11. this.GET = req._GET;
  12. this.POST = req._POST;
  13. this.UPLOAD = req._UPLOAD;
  14. /**
  15. * [response description]
  16. * @return {[type]} [description]
  17. */
  18. this.end = this.response = function(){
  19. let mimeType = 'text/plain';
  20. let status = null;
  21. let endContent = '';
  22. switch(arguments.length){
  23. case 1:
  24. endContent = arguments[0];
  25. break;
  26. case 2:
  27. status = arguments[0];
  28. endContent = arguments[1];
  29. break;
  30. case 3:
  31. status = arguments[0];
  32. endContent = arguments[1];
  33. mimeType = arguments[2];
  34. break;
  35. }
  36. if(status) this.Response.statusCode = status;
  37. if(mimeType) this.Response.setHeader('Content-Type', `${mimeType}; charset=UTF-8`);
  38. this.Response.write(endContent);
  39. this.Response.end();
  40. }
  41. this.endRedirect = this.responseRedirect = function(location,permanently){
  42. let status = permanently?301:302;
  43. this.Response.writeHead(status,{'Location':location});
  44. this.Response.end();
  45. }
  46. /**
  47. * [responseInJSON description]
  48. * @param {[type]} somthing [String,Number,Object]
  49. */
  50. this.endJSON = this.responseInJSON = function(somthing){
  51. let endContent;
  52. if(typeof somthing == 'string' || typeof somthing == 'number'){
  53. endContent = {data:somthing};
  54. }else if(typeof somthing == 'object'){
  55. endContent = somthing;
  56. }else{
  57. return LOGGER.error('Function endInJSON argument type must be string,number or object');
  58. }
  59. this.end(200,JSON.stringify(endContent),'text/json');
  60. }
  61. /**
  62. * [responseAPI description]
  63. * @param {[type]} statusCode [description]
  64. * @param {[type]} somthing [description]
  65. */
  66. this.endAPI = this.responseAPI = function(statusCode,somthing){
  67. let endContent = {statusCode:statusCode,data:null};
  68. if(typeof somthing == 'string' || typeof somthing == 'number' || typeof somthing == 'object'){
  69. endContent.data = somthing;
  70. this.end(200,JSON.stringify(endContent),'text/json');
  71. }else{
  72. return LOGGER.error('Function endAPI argument type must be string,number or object');
  73. }
  74. }
  75. /**
  76. * [responseView description]
  77. * @param {[type]} viewName [description]
  78. * @param {[type]} data [description]
  79. * @return {[type]} [description]
  80. */
  81. this.endView = this.responseView = function(viewName,data){
  82. let view = new VIEW(viewName,data);
  83. if(view && view.html){
  84. this.end(200,view.html,'text/html');
  85. }else{
  86. this.end(502,`View [${viewName}] not found`,'text/plain');
  87. }
  88. }
  89. this.setCookie = function(key,value,expire,path){
  90. let cookie,originCookie;
  91. if(this.Response.hasHeader('Set-Cookie')){
  92. originCookie = this.Response.getHeader('Set-Cookie');
  93. }
  94. if(!originCookie) originCookie = [];
  95. if(typeof originCookie == 'string') originCookie = [originCookie];
  96. cookie = `${key}=${value}`;
  97. if(path) cookie += ';path=' + path;
  98. if(expire){
  99. let expireTime;
  100. if(expire=='forever'){
  101. expireTime = new Date('9999/01/01 00:00:00').toGMTString();
  102. }else{
  103. expireTime = new Date(new Date().getTime()+Number(expire)*1000*60).toGMTString();
  104. }
  105. cookie += ';expires=' + expireTime;
  106. }
  107. originCookie.push(cookie);
  108. this.Response.setHeader('Set-Cookie',cookie);
  109. }
  110. this.renewCookie = function(key,expire,path){
  111. let originCookieValue = this.COOKIE[key];
  112. if(!originCookieValue) return false;
  113. this.setCookie(key,originCookieValue,expire,path);
  114. }
  115. }