/** * @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 REFERER:string; public REQPATH:string; public REQMETHOD:string; public REQTIME:number; public COOKIE:any; public GET:any; public POST:any; public UPLOAD:any; public SESSION:any; public DATA:AnyKeyString; constructor(requestData:RequestData) { this._requestData = requestData; this._Request = requestData.req; this._Response = requestData.res; this.REFERER = this._Request.headers.referer; this.REQPATH = requestData.path||''; this.REQMETHOD = requestData.method; this.REQTIME = Moment().unix(); this.COOKIE = requestData.COOKIE; this.GET = requestData.GET; this.POST = requestData.POST; this.UPLOAD = requestData.UPLOAD; this.SESSION = requestData.SESSION; this.DATA = {}; 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(renderedTpl,200,'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,httpOnly?:boolean){ 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}`; cookie += ';path=' + (path||'/'); expireHour = expireHour!=undefined&&expireHour>=0?expireHour:'forever'; let expireTime; if(expireHour=='forever'){ expireTime = new Date('9999/01/01 00:00:00').toUTCString(); }else if(expireHour==0){ expireTime = new Date(0).toUTCString(); }else{ expireTime = new Date(new Date().getTime()+Number(expireHour)*1000*60).toUTCString(); } cookie += ';expires=' + expireTime; if(httpOnly) cookie += ';httpOnly=true'; 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'); } }