Daroute.js
Introduction
Daroute is a node.js routing framework and path analizer module.
- define simple or dynamic routes (with or without parser)
- find the best route who match, if no route found throw a NotFound error
- mineType used to serve static files
- define custom errors
- handle error
- complete and colored console log
- ...
Here is an example on how to use it:
var Daroute = ;; // try with http://127.0.0.1:8080/totoDaroute;
Example for static files:
// http://127.0.0.1:8080/static/sound/test.mp3 serve the file /v1_alpha/static/sound/test.mp3Daroute;
Example for dynamic urls:
// try with /forum/name-of-the-question-1/5,78?order=descDaroute;
add routes
When a user arrive on this adresse path, Daroute found the most complex route, if no route found throw a NotFound error. If Daroute found a route, the callback is false, the path is served like a static files. You can add some variable parts in the route. The variable parts are content between < and > (Eg. <toto> ). The variable parts can be defined a specific type predefined (Eg. <int:toto> ). Diffrents types already available: int, float, hexa, alnum, path, list_int, but you can defined your specific parser for your custom types.
All route are sorted by complexity (by default: int, float, list_int...). The experts can redefined they custom Daroute.sort method.
Arguments is on request.params
- request.params.route: Arguments from the route
- request.params.get : Arguments from the querystring
- request.params.post : Arguments from the post form (use enctype='multipart/form-data' for your HTML form)
Daroute;// try with http://127.0.0.1:8080/my/route/55/--*98fs+%20--/truc5,6,8,78// request.params contain { get: {}, route: {lou: 55, bobo: '--*98fs+%20--', pepe: [5,6,8,78]}, post: {} }
route options: encoding, cache, headers...
You can define for all routes (static or not) some options like:
- encoding: automatically encode the result (static file or method result) in function of the client browser ('deflate' or 'gzip').
- cache: cache the route result server side (useful for performance especially for minifications or encoding)
- headers: set default headers (you can overwrite it in the function of the no static routes)
- ...
// define a static route link to an other alpha folder// http://127.0.0.1:8080/static/sound/test.mp3 serve the file /v1_alpha/static/sound/test.mp3Daroute;
route are sorted by complexity
Daroute;Daroute;// try with http://127.0.0.1:8080/my/route/55// lou2 is called and request.params.route.lou is an integer with a value = 55
defined your specific parser for your custom types
The parsers receive all the regex match as first value and can receive an optional second argument from the data surrounded by parentheses in the route
// add a new path parser for the routes// value: path who match with the regexp (semantic URL to improve SEO for eg.)// model: data surrounded by parentheses in the routeDaroute; // define route with custom exeptionDaroute;// try with http://127.0.0.1:8080/name-user-65/other-name-45?user=77// request.params contain: { get: {user: '77'}, route: { bobo: [DB object 65], user: [DB object 45] }, post: {} }
onRequest, onBegin, onEnd, onError, onFinish
Daroute call all your onBegin method before call a route method Daroute call all your onEnd method if they are no error in the route method Daroute call all your onError method if they are a least one error catched in the route method
Daroute;Daroute;Daroute;Daroute;Daroute;
Errors handler
Define your custom errors to simplify and improve security of your application and get a good log. When we throw a custom error, two log are created.
- log with level defined, name, date and content of your error.
- debug log display the traceback of your error
All non custom errors or predefined errors (BadRequest, ValueError, TypeError, AccessError, AccessDenied, NotFound), are logged with error level and contain the traceback
Daroute; // define a callback for a custom exeptionDaroute; // In your routethrow "Message to return for the log. You can use %s, or the arguments is added at the end" data other_data;
Logger
- Colored message in your console
- 4 log levels : 'error', 'warn', 'info', 'debug'
- Arguments are converted with util.format and logged with console.log
- Defined the minimal log level to display
The log displayed content:
- date (white color)
- logger name (blue color)
- log level (color in function of level)
- message & data
// define a new logger (level 3 = display debug, info, warning and error log)var logger1 = "main.js" level: 3;// define a new logger (level 1 = display warning and error log)var logger2 = "other app" level: 1; logger1;// console: 2013-09-28 13:46:08 main.js - DEBUG - Debug message and datalogger1;// console: 2013-09-28 13:46:08 main.js - INFO - Informations eg. 55 { yep: 44 }logger2;// nothing displayedlogger2;// console: 2013-09-28 13:46:08 other app - WARN - Warning messagelogger2;// console: 2013-09-28 13:46:08 other app - ERROR - Error message
MineType
The list of mineTypes is defined in mineType.data You can defined your mineType in your application
Daroute// return "application/javascript"Daroute