123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /**
- * You can visit URL:
- * http://127.0.0.1:8080/example or http://127.0.0.1:8080/example/endWithStatusCode
- * for test
- */
- export default class Example extends CONTROLLER{
- private status:string | undefined;
- /**
- * This constructor will be called when controller init,you can ignore it if not useful;
- * DO NOT User function {constructor} for instead!!!
- */
- __construct(){
- this.status = 'SUCCESS';
- }
- /**
- * This method shows how to end a request
- */
- index(){
- this.end(this.status);
- }
- /**
- * End a request with http status code
- */
- endWithStatusCode(){
- this.end(this.status,403);
- }
- /**
- * End a request sepcify different mime type
- */
- endWithMimetype(){
- this.end(this.status,200,'text/html');
- }
- /**
- * End with redirect (permanently or not)
- */
- redirect(){
- /**
- * Permanently StatusCode:
- * True: 301
- * False: 302
- */
- const permanently = true;
- this.endRedirect('/example',permanently);
- }
- /**
- * End with a raw json type string
- */
- endRawJson(){
- // Default end a json string with mimeType 'text/json' response
- this.endJSON({status:this.status});
- /**
- * You can just return a string or number type data
- * Controller will automake json data like {data:{Your input data}}
- */
- // this.endJSON(this.status);
- // this.endJSON(0xff)
- }
- /**
- * End for a api request
- */
- endForAPI(){
- /**
- * Controller will automake json data like {error:{ErrorCode},data:{Your Data}} and return a 'text/json' mime type response
- */
- this.endAPI(0,{status:this.status});
- }
- /**
- * End a html template
- * Data will be processed by EJS
- */
- endWithViewTpl(){
- //Controller will auto find html template file in '{Your project dist folder}/app/mvc/view' by the given name
- this.endView('example',{data:this.status});
- }
- /**
- * This shows you how to set cookies
- */
- setSomeCookies(){
- //Set cookie key -> value
- this.setCookie('status','SUCCESS');
- //Set cookie with expire time (Hour)
- this.setCookie('status','SUCCESS',24);
- //Set cookie with no outdate
- this.setCookie('status','SUCCESS','forever');
- //Set cookie path
- this.setCookie('status','SUCCESS','forever','/');
- //Don't forget end
- this.end(this.status);
- }
- renewSomeCookie(){
- //Renew cookie expire time
- this.renewCookie('status',24);
- //With forever
- this.renewCookie('status','forever');
- //Reset path
- this.renewCookie('status','forever','/');
- //Don't forget end
- this.end(this.status);
- }
- }
|