/** * 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); } }