/** * @Author HonorLee (dev@honorlee.me) * @Version 1.0 (2018-05-04) * @License MIT */ 'use strict' module.exports = function(req,res){ this.Request = req; this.Response = res; this.COOKIE = req._Cookie; this.GET = req._GET; this.POST = req._POST; this.UPLOAD = req._UPLOAD; /** * [response description] * @return {[type]} [description] */ this.end = this.response = function(){ let mimeType = 'text/plain'; let status = null; let endContent = ''; switch(arguments.length){ case 1: endContent = arguments[0]; break; case 2: status = arguments[0]; endContent = arguments[1]; break; case 3: status = arguments[0]; endContent = arguments[1]; mimeType = arguments[2]; break; } if(status) this.Response.statusCode = status; if(mimeType) this.Response.setHeader('Content-Type', `${mimeType}; charset=UTF-8`); this.Response.write(endContent); this.Response.end(); } this.endRedirect = this.responseRedirect = function(location,permanently){ let status = permanently?301:302; this.Response.writeHead(status,{'Location':location}); this.Response.end(); } /** * [responseInJSON description] * @param {[type]} somthing [String,Number,Object] */ this.endJSON = this.responseInJSON = function(somthing){ let endContent; if(typeof somthing == 'string' || typeof somthing == 'number'){ endContent = {data:somthing}; }else if(typeof somthing == 'object'){ endContent = somthing; }else{ return LOGGER.error('Function endInJSON argument type must be string,number or object'); } this.end(200,JSON.stringify(endContent),'text/json'); } /** * [responseAPI description] * @param {[type]} statusCode [description] * @param {[type]} somthing [description] */ this.endAPI = this.responseAPI = function(statusCode,somthing){ let endContent = {statusCode:statusCode,data:null}; if(typeof somthing == 'string' || typeof somthing == 'number' || typeof somthing == 'object'){ endContent.data = somthing; this.end(200,JSON.stringify(endContent),'text/json'); }else{ return LOGGER.error('Function endAPI argument type must be string,number or object'); } } /** * [responseView description] * @param {[type]} viewName [description] * @param {[type]} data [description] * @return {[type]} [description] */ this.endView = this.responseView = function(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'); } } this.setCookie = function(key,value,expire,path){ 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(expire){ let expireTime; if(expire=='forever'){ expireTime = new Date('9999/01/01 00:00:00').toGMTString(); }else{ expireTime = new Date(new Date().getTime()+Number(expire)*1000*60).toGMTString(); } cookie += ';expires=' + expireTime; } originCookie.push(cookie); this.Response.setHeader('Set-Cookie',cookie); } this.renewCookie = function(key,expire,path){ let originCookieValue = this.COOKIE[key]; if(!originCookieValue) return false; this.setCookie(key,originCookieValue,expire,path); } }