/** * @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 = 200; 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; } this.Response.writeHead(status, { 'Content-Type': `${mimeType}; charset=UTF-8`}); this.Response.write(endContent); this.Response.end(); } this.endRedirect = this.responseRedirect = function(status,location){ 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(403,`View [${viewName}] not found`); } } }