/** * @Author HonorLee (dev@honorlee.me) * @Version 1.0 (2018-05-04) * @License MIT */ export default class Controller{ private _requestData:RequestData; private _Request:any; private _Response:any; public COOKIE:any; public GET:any; public POST:any; public UPLOAD:any; constructor(requestData:RequestData) { this._requestData = requestData; this._Request = requestData.req; this._Response = requestData.res; this.COOKIE = requestData.COOKIE; this.GET = requestData.GET; this.POST = requestData.POST; this.UPLOAD = requestData.UPLOAD; if(this.GET._time) delete this.GET._time; if(this.POST._time) delete this.POST._time; // console.log(this._constructor) } /** * [response description] * @return {[type]} [description] */ end(data:any,status?:number,mime?:string):void{ let endContent = data,statusCode = 200,mimeType = 'text/plain'; if(!endContent) endContent = ''; if(typeof endContent == 'number') endContent = `${endContent}`; if(status) statusCode = status; if(mime) mimeType = mime; if(status) this._Response.statusCode = status; if(mimeType) this._Response.setHeader('Content-Type', `${mimeType}; charset=UTF-8`); try{ this._Response.write(endContent); this._Response.end(); }catch(e:any){ LOGGER.error(e.stack) } } endRedirect(location:string,permanently?:boolean){ const status = permanently?301:302; this._Response.writeHead(status,{'Location':location}); this._Response.end(); } /** * [responseInJSON description] * @param {[type]} somthing [String,Number,Object] */ endJSON(somthing:any){ let endContent; if(typeof somthing == 'string' || typeof somthing == 'number'){ endContent = {data:somthing}; }else if(typeof somthing == 'object'){ endContent = somthing; }else{ return this._error(new Error('Function endInJSON argument type must be string,number or object')); } this.end(JSON.stringify(endContent),200,'text/json'); } /** * [responseAPI description] * @param {[type]} errorCode [description] * @param {[type]} somthing [description] */ endAPI(errorCode:number,somthing:any){ const endContent = {error:errorCode,data:null}; if(typeof somthing == 'string' || typeof somthing == 'number' || typeof somthing == 'object'){ endContent.data = somthing; this.end(JSON.stringify(endContent),200,'text/json'); }else{ this._error(new Error('Function endAPI argument type must be string,number or object')); } } /** * [responseAPI description] * @param {[type]} errorCode [description] * @param {[type]} somthing [description] */ endView(viewname:string,data:any,option?:any){ const viewFilePath = `${SYSTEM.MVC.PATH.VIEW}/${viewname}`; let tpl; try{ tpl = FILE.readFileSync(`${viewFilePath}.html`,'utf-8'); }catch(e){ try{ tpl = FILE.readFileSync(`${viewFilePath}.tpl`,'utf-8'); }catch(e){ } } if(!tpl) return this.end(`View template [${viewname}] not exist!`); const render = SYSTEM.MVC.EJS.compile(tpl,option); const renderedTpl = render(data); this.end(200,renderedTpl,'text/html'); } /** * [responseView description] * @param {[type]} viewName [description] * @param {[type]} data [description] * @return {[type]} [description] */ // endView(viewName,data){ // let view = new VIEW(viewName,data); // if(view && view.html){ // this.end(200,view.html,'text/html'); // }else{ // this.end(502,`View [${viewName}] not found`,'text/plain'); // } // } setCookie(key:string,value:any,expireHour?:number|'forever',path?:string){ let cookie,originCookie; if(this._Response.hasHeader('Set-Cookie')){ originCookie = this._Response.getHeader('Set-Cookie'); } if(!originCookie) originCookie = []; if(typeof originCookie == 'string') originCookie = [originCookie]; cookie = `${key}=${value}`; if(path) cookie += ';path=' + path; if(expireHour){ let expireTime; if(expireHour=='forever'){ expireTime = new Date('9999/01/01 00:00:00').toUTCString(); }else{ expireTime = new Date(new Date().getTime()+Number(expireHour)*1000*60).toUTCString(); } cookie += ';expires=' + expireTime; } originCookie.push(cookie); this._Response.setHeader('Set-Cookie',cookie); } renewCookie(key:string,expire:number|'forever',path?:string){ const originCookieValue = this.COOKIE[key]; if(!originCookieValue) return false; this.setCookie(key,originCookieValue,expire,path); } _error(err:any){ LOGGER.error(err.stack); this.end(`
- 403 -
`,403,'text/html'); } }