example.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * You can visit URL:
  3. * http://127.0.0.1:8080/example or http://127.0.0.1:8080/example/endWithStatusCode
  4. * for test
  5. */
  6. export default class Example extends CONTROLLER{
  7. private status:string | undefined;
  8. /**
  9. * This constructor will be called when controller init,you can ignore it if not useful;
  10. * DO NOT User function {constructor} for instead!!!
  11. */
  12. __construct(){
  13. this.status = 'SUCCESS';
  14. }
  15. /**
  16. * This method shows how to end a request
  17. */
  18. index(){
  19. this.end(this.status);
  20. }
  21. /**
  22. * End a request with http status code
  23. */
  24. endWithStatusCode(){
  25. this.end(this.status,403);
  26. }
  27. /**
  28. * End a request sepcify different mime type
  29. */
  30. endWithMimetype(){
  31. this.end(this.status,200,'text/html');
  32. }
  33. /**
  34. * End with redirect (permanently or not)
  35. */
  36. redirect(){
  37. /**
  38. * Permanently StatusCode:
  39. * True: 301
  40. * False: 302
  41. */
  42. const permanently = true;
  43. this.endRedirect('/example',permanently);
  44. }
  45. /**
  46. * End with a raw json type string
  47. */
  48. endRawJson(){
  49. // Default end a json string with mimeType 'text/json' response
  50. this.endJSON({status:this.status});
  51. /**
  52. * You can just return a string or number type data
  53. * Controller will automake json data like {data:{Your input data}}
  54. */
  55. // this.endJSON(this.status);
  56. // this.endJSON(0xff)
  57. }
  58. /**
  59. * End for a api request
  60. */
  61. endForAPI(){
  62. /**
  63. * Controller will automake json data like {error:{ErrorCode},data:{Your Data}} and return a 'text/json' mime type response
  64. */
  65. this.endAPI(0,{status:this.status});
  66. }
  67. /**
  68. * End a html template
  69. * Data will be processed by EJS
  70. */
  71. endWithViewTpl(){
  72. //Controller will auto find html template file in '{Your project dist folder}/app/mvc/view' by the given name
  73. this.endView('example',{data:this.status});
  74. }
  75. /**
  76. * This shows you how to set cookies
  77. */
  78. setSomeCookies(){
  79. //Set cookie key -> value
  80. this.setCookie('status','SUCCESS');
  81. //Set cookie with expire time (Hour)
  82. this.setCookie('status','SUCCESS',24);
  83. //Set cookie with no outdate
  84. this.setCookie('status','SUCCESS','forever');
  85. //Set cookie path
  86. this.setCookie('status','SUCCESS','forever','/');
  87. //Don't forget end
  88. this.end(this.status);
  89. }
  90. renewSomeCookie(){
  91. //Renew cookie expire time
  92. this.renewCookie('status',24);
  93. //With forever
  94. this.renewCookie('status','forever');
  95. //Reset path
  96. this.renewCookie('status','forever','/');
  97. //Don't forget end
  98. this.end(this.status);
  99. }
  100. }