Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Ni.addRoute(source, dest [, method]);

  • Loading branch information...
commit 015e664300c4c1962930708c98c98088abc86dbc 1 parent da1d014
@maritz maritz authored
Showing with 77 additions and 2 deletions.
  1. +77 −2 lib/ni.js
View
79 lib/ni.js
@@ -60,6 +60,9 @@ var Ni = function() {
// default view directory name
this.config('view_dir', 'views');
+ // default view directory name
+ this.config('custom_routes', []);
+
/*
* Loads controllers, models, views, templates and helpers from the root
* directory, and makes them all available to the Ni object.
@@ -105,11 +108,15 @@ var Ni = function() {
this.router = function(req, res, next) {
var parsedUrl = require('url').parse(req.url, true),
- pathArr = parsedUrl.pathname.split('/'),
- args = pathArr.slice(3),
+ pathArr,
+ args,
controller,
fn;
+ parsedUrl = Ni.checkCustomRoutes(parsedUrl.pathname, req.method);
+ pathArr = parsedUrl.split('/');
+ args = pathArr.slice(3);
+
res.Ni = { view: Ni.config('automatic_views') };
args.unshift(next);
@@ -169,6 +176,74 @@ var Ni = function() {
}
+ /*
+ * Add a custom route with a source (can be string, regex or function) and destination (string).
+ *
+ * If a route matches, the path is rewritten to the specified destination.
+ *
+ * A string route matches if it is exactly the same as the path. It rewrites to exactly the given destination.
+ * A regex route matches if it matches the path. It rewrites to path.replace(source, destination).
+ * A function matches if - given the path and custom route object - it returns a non-falsy value. It rewrites to the return of the function if that is a string or to the given destination.
+ */
+ this.addRoute = function (src, dest, method) {
+ if (arguments.length < 2) {
+ throw new Error('Ni.addRoute expects 2 arguments - '+arguments.length+' given.');
+ }
+ var routes = Ni.config('custom_routes') || [];
+ routes.push({
+ src: src,
+ dest: dest,
+ method: method || false
+ });
+ Ni.config('custom_routes', routes);
+ }
+
+ /*
+ * Checks the given url for the first matching custom route.
+ */
+ this.checkCustomRoutes = function (path, method) {
+ var routes = Ni.config('custom_routes');
+ if (!Array.isArray(routes) || routes.length === 0)
+ return path;
+ for (var i = 0, len = routes.length; i < len; i++) {
+ var route = routes[i]
+ , type = typeof(route.src),
+ methodMatch = !route.method; // if no method is specified in the route, automatically set to matched
+
+ if (!methodMatch && Array.isArray(route.method)) {
+ for (var n = 0, lenn = route.method.length; n < lenn; n++) {
+ if (route.method[n].toUpperCase() === method)
+ methodMatch = true;
+ }
+ } else if (!methodMatch) {
+ methodMatch = route.method === method;
+ }
+ if (methodMatch && type !== 'undefined') {
+ switch(type) {
+ case 'string':
+ if (path === route.src) {
+ return route.dest;
+ }
+ break;
+ case 'function':
+ if (typeof(route.src.exec) !== 'undefined') {
+ // regex
+ if (route.src.exec(path)) {
+ return path.replace(route.src, route.dest);
+ }
+ } else {
+ // function
+ var result = route.src(path, route);
+ if (result) {
+ return typeof(result) === 'string' ? result : route.dest;
+ }
+ }
+ }
+ }
+ }
+ return path;
+ }
+
this.renderView = function (renderer) {
if (typeof(renderer) === 'function') {
Please sign in to comment.
Something went wrong with that request. Please try again.