Comparing version 0.1.0-alpha-4 to 0.1.0-alpha-6
@@ -44,2 +44,3 @@ "use strict"; | ||
* On request handle favicon | ||
* @return {object} | ||
*/ | ||
@@ -53,5 +54,13 @@ onRequest: function Favicon_onRequest(api) { | ||
file = this.readFile(filePath); | ||
mimeType = this.mimeType(filePath); | ||
if (!mimeType) { | ||
logger.print('MimeType', mimeType, filePath); | ||
return false; | ||
} | ||
file = this.readFile(filePath); | ||
api.addHeader('Content-Type', mimeType); | ||
@@ -58,0 +67,0 @@ api.addHeader('Cache-Control', 'public, max-age=' + ~~(maxAge)); |
@@ -29,2 +29,3 @@ "use strict"; | ||
response: Type.OBJECT, | ||
url: Type.STRING, | ||
parsedUrl: Type.OBJECT, | ||
@@ -37,5 +38,5 @@ route: Type.STRING, | ||
statusCode: Type.NUMBER, | ||
forwardUrl: Type.STRING, | ||
headers: Type.OBJECT, | ||
isRendered: Type.BOOLEAN | ||
isRendered: Type.BOOLEAN, | ||
isERROR: Type.BOOLEAN | ||
}, { | ||
@@ -46,3 +47,4 @@ _construct: function Request(config, url) { | ||
this.headers = {}; | ||
this.parsedUrl = URLParser.parse(url, true); | ||
this.url = url; | ||
this.parsedUrl = URLParser.parse(this.url, true); | ||
}, | ||
@@ -149,2 +151,3 @@ /** | ||
* Return header | ||
* @return {object} | ||
*/ | ||
@@ -199,5 +202,10 @@ getHeader: function Request_getHeader(key) { | ||
forward: function Request_forward(route, params) { | ||
var request; | ||
if (router.trim(this.route, "/") === router.trim(route, '/')) { | ||
throw new error.HttpError(500, {route: route, params: params}, 'Cannot forward to same route'); | ||
throw new error.HttpError(500, { | ||
route: route, | ||
params: params | ||
}, 'Cannot forward to same route'); | ||
} else { | ||
@@ -248,2 +256,36 @@ | ||
* @author Igor Ivanovic | ||
* @method Request#getMethod | ||
* | ||
* @description | ||
* Return current request method | ||
*/ | ||
getMethod: function Request_getMethod() { | ||
return this.request.method; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#parseRequest | ||
* | ||
* @description | ||
* Parse request | ||
*/ | ||
parse: function Request_parse() { | ||
return hooks.process(this._getApi()) | ||
.then(function handleHooks(data) { | ||
if (Type.isInitialized(data) && !!data) { | ||
return data; | ||
} | ||
return router | ||
.process(this.request.method, this.parsedUrl) // find route | ||
.then(this._resolveRoute.bind(this), this._handleError.bind(this)); // resolve route chain | ||
}.bind(this), this._handleError.bind(this)) | ||
.then(this._render.bind(this), this._handleError.bind(this)) // render chain | ||
.then(this._render.bind(this), this._handleError.bind(this)); // render error thrown in render function | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#_checkContentType | ||
@@ -259,5 +301,64 @@ * | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#_handleError | ||
* | ||
* @description | ||
* Handle error | ||
*/ | ||
_handleError: function Request_handleError(response) { | ||
var Controller, | ||
errorRoute = router.getErrorRoute(), | ||
errorController = '@{controllersPath}/' + errorRoute.shift(), | ||
errorAction = errorRoute.shift(); | ||
if (response instanceof Error && !this.isERROR && di.exists(errorController, '.js')) { | ||
if (response.code) { | ||
this.statusCode = response.code; | ||
} else { | ||
this.statusCode = 500; | ||
} | ||
try { | ||
Controller = di.load(errorController); | ||
errorController = new Controller(); | ||
if (errorController.hasAction('action_' + errorAction)) { | ||
response = errorController.getAction('action_' + errorAction)(response); | ||
if (response.trace) { | ||
this._render(response.trace); | ||
} else if (response.stack) { | ||
this._render(response.stack); | ||
} else { | ||
this._render(util.inspect(response)); | ||
} | ||
} else { | ||
logger.print('Request.render: no error controller provided', errorController); | ||
} | ||
} catch (e) { | ||
this.isERROR = true; | ||
throw new error.HttpError(500, {}, "Error on executing error action", e); | ||
} | ||
} else if (response.trace) { | ||
this.addHeader('Content-Type', 'text/plain'); | ||
this._render(response.trace); | ||
} else if (response.stack) { | ||
this.addHeader('Content-Type', 'text/plain'); | ||
this._render(response.stack); | ||
} else if (this.isERROR) { | ||
this.addHeader('Content-Type', 'text/plain'); | ||
this._render(util.inspect(response)); | ||
} else { | ||
return this._render(response); | ||
} | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#end | ||
@@ -276,16 +377,5 @@ * | ||
if (response instanceof Error) { | ||
if (response.code) { | ||
this.statusCode = response.code; | ||
} else { | ||
this.statusCode = 500; | ||
} | ||
this.response.writeHead(this.statusCode, this.headers); | ||
if (response.trace) { | ||
this.response.end(response.trace); | ||
} else { | ||
this.response.end(util.inspect(response)); | ||
} | ||
this.response.writeHead(this.statusCode, this.headers); | ||
} else if (Type.isString(response)) { | ||
if (Type.isString(response)) { | ||
this.addHeader('Content-Length', response.length); | ||
@@ -299,22 +389,11 @@ this.response.end(response); | ||
} else { | ||
throw new error.HttpError(500, {}, 'Invalid response type, it must be string!!'); | ||
throw new error.HttpError(500, {}, 'Invalid response type, string or buffer is required!'); | ||
} | ||
} | ||
this.isRendered = true; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#getMethod | ||
* | ||
* @description | ||
* Return current request method | ||
*/ | ||
getMethod: function Request_getMethod() { | ||
return this.request.method; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#getApi | ||
@@ -440,25 +519,3 @@ * | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#parseRequest | ||
* | ||
* @description | ||
* Parse request | ||
*/ | ||
parse: function Request_parse() { | ||
return hooks.process(this._getApi()) | ||
.then(function handleHooks(data) { | ||
if (Type.isInitialized(data) && !!data) { | ||
return data; | ||
} | ||
return router | ||
.process(this.request.method, this.parsedUrl) // find route | ||
.then(this._resolveRoute.bind(this), this._handleError.bind(this)); // resolve route chain | ||
}.bind(this), this._handleError.bind(this)) | ||
.then(this._render.bind(this), this._handleError.bind(this)) // render chain | ||
.then(this._render.bind(this), this._handleError.bind(this)); // render error thrown in render function | ||
}, | ||
/** | ||
@@ -486,16 +543,4 @@ * @since 0.0.1 | ||
return this._handleRoute(); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Request#handleError | ||
* | ||
* @description | ||
* Handle error | ||
*/ | ||
_handleError: function Request_handleError(data) { | ||
this.statusCode = 500; | ||
this._checkContentType('text/plain'); | ||
return this._render(data); | ||
} | ||
}); | ||
@@ -502,0 +547,0 @@ |
@@ -29,7 +29,9 @@ "use strict"; | ||
this.routes = []; | ||
this.config = { | ||
defaultRoute: "home/index", | ||
this.config = core.extend({ | ||
errorRoute: "error/index" | ||
}; | ||
core.extend(this.config, config); | ||
}, config); | ||
if (!Type.assert(Type.STRING, this.config.errorRoute)) { | ||
throw new error.DataError(this.config, 'Router.construct: errorRoute must be string type'); | ||
} | ||
}, | ||
@@ -43,5 +45,6 @@ /** | ||
* Get default error route | ||
* @return {object} | ||
*/ | ||
getErrorRoute: function Router_getErrorRoute() { | ||
return this.config.errorRoute; | ||
return this.config.errorRoute.split('/').splice(0, 2); | ||
}, | ||
@@ -51,13 +54,2 @@ /** | ||
* @author Igor Ivanovic | ||
* @method Router#getDefaultRoute | ||
* | ||
* @description | ||
* Returns default route | ||
*/ | ||
getDefaultRoute: function Router_getDefaultRoute() { | ||
return this.config.defaultRoute; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Router#add | ||
@@ -100,2 +92,3 @@ * | ||
* Create url | ||
* @return {string} | ||
*/ | ||
@@ -170,2 +163,3 @@ createUrl: function Router_createUrl(route, params) { | ||
* Build query string | ||
* @return {string} | ||
*/ | ||
@@ -172,0 +166,0 @@ buildQuery: function Router_buildQuery(params) { |
@@ -14,7 +14,7 @@ "use strict"; | ||
* @author Igor Ivanovic | ||
* @name Loader | ||
* @name DI | ||
* | ||
* @constructor | ||
* @description | ||
* Loader is main class which provide all paths to load part of application | ||
* DI is main class for handling dependency injection | ||
*/ | ||
@@ -38,3 +38,3 @@ var DI = Type.create({ | ||
* @author Igor Ivanovic | ||
* @method Loader#hasAlias | ||
* @method DI#hasAlias | ||
* | ||
@@ -50,3 +50,3 @@ * @description | ||
* @author Igor Ivanovic | ||
* @method Loader#getAlias | ||
* @method DI#getAlias | ||
* | ||
@@ -67,3 +67,3 @@ * @description | ||
* @author Igor Ivanovic | ||
* @method Loader#setAlias | ||
* @method DI#setAlias | ||
* | ||
@@ -85,3 +85,3 @@ * @description | ||
* @author Igor Ivanovic | ||
* @method Loader#normalizePath | ||
* @method DI#normalizePath | ||
* | ||
@@ -100,3 +100,3 @@ * @description | ||
* @author Igor Ivanovic | ||
* @method Loader#readFileSync | ||
* @method DI#readFileSync | ||
* | ||
@@ -117,5 +117,19 @@ * @description | ||
* @author Igor Ivanovic | ||
* @method Loader#load | ||
* @method DI#exists | ||
* | ||
* @description | ||
* Check if file exists | ||
*/ | ||
exists: function DI_exists(file, fileType) { | ||
if (!Type.isString(fileType) && !Type.isString(file)) { | ||
throw new error.DataError({file: file, fileType: fileType}, 'DI.exists: file or fileType must bi string'); | ||
} | ||
return fs.existsSync(this.normalizePath(file) + fileType); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method DI#load | ||
* | ||
* @description | ||
* Load an package | ||
@@ -122,0 +136,0 @@ */ |
@@ -5,3 +5,3 @@ { | ||
"description": "Powerful lightweight mvc framework for nodejs", | ||
"version": "0.1.0-alpha-4", | ||
"version": "0.1.0-alpha-6", | ||
"dependencies" : { | ||
@@ -8,0 +8,0 @@ "mongoose": "3.8.x", |
94163
3053