handler.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = 200;
  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. this.Response.writeHead(status, { 'Content-Type': `${mimeType}; charset=UTF-8`});
  37. this.Response.write(endContent);
  38. this.Response.end();
  39. }
  40. this.endRedirect = this.responseRedirect = function(status,location){
  41. this.Response.writeHead(status,{'Location':location});
  42. this.Response.end();
  43. }
  44. /**
  45. * [responseInJSON description]
  46. * @param {[type]} somthing [String,Number,Object]
  47. */
  48. this.endJSON = this.responseInJSON = function(somthing){
  49. let endContent;
  50. if(typeof somthing == 'string' || typeof somthing == 'number'){
  51. endContent = {data:somthing};
  52. }else if(typeof somthing == 'object'){
  53. endContent = somthing;
  54. }else{
  55. return LOGGER.error('Function endInJSON argument type must be string,number or object');
  56. }
  57. this.end(200,JSON.stringify(endContent),'text/json');
  58. }
  59. /**
  60. * [responseAPI description]
  61. * @param {[type]} statusCode [description]
  62. * @param {[type]} somthing [description]
  63. */
  64. this.endAPI = this.responseAPI = function(statusCode,somthing){
  65. let endContent = {statusCode:statusCode,data:null};
  66. if(typeof somthing == 'string' || typeof somthing == 'number' || typeof somthing == 'object'){
  67. endContent.data = somthing;
  68. this.end(200,JSON.stringify(endContent),'text/json');
  69. }else{
  70. return LOGGER.error('Function endAPI argument type must be string,number or object');
  71. }
  72. }
  73. /**
  74. * [responseView description]
  75. * @param {[type]} viewName [description]
  76. * @param {[type]} data [description]
  77. * @return {[type]} [description]
  78. */
  79. this.endView = this.responseView = function(viewName,data){
  80. let view = new VIEW(viewName,data);
  81. if(view && view.html){
  82. this.end(200,view.html,'text/html');
  83. }else{
  84. this.end(403,`View [${viewName}] not found`);
  85. }
  86. }
  87. }