Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add readme for Ni.addRoute

Fix a few problems with Ni.addRoute
  • Loading branch information...
commit b17ec240e45aa4b38d85c6633d5119c97bb68cda 1 parent 015e664
Moritz Peters maritz authored
Showing with 40 additions and 3 deletions.
  1. +34 −0 README.md
  2. +6 −3 lib/ni.js
34 README.md
View
@@ -49,6 +49,40 @@ If no controller is specified (`http://myapp.com/`), it loads the `home` control
If no function is specified (http://yourapp.com/[controller]), it loads the `[controller]`'s `index` function.
+You can define custom routes using Ni.addRoute(source, destination[, method]);
+
+Some examples:
+
+With this custom route calling myapp.com will internally redirect to use your News controller and call the index function on it:
+ Ni.addRoute('/', '/News/index');
+
+You can use regular expressions as well. This leads myapp.com/register to your User controller and its "register" function:
+ Ni.addRoute(/^\/register/i, '/User/register');
+
+If you want to use arguments with custom routes, you can do that as well:
+ Ni.addRoute(/^\/details\/([\d]+)/i, '/User/details/$1');
+
+You can also define functions to test the path. This example will add the first and second path variables.
+Calling myapp.com/1/2 will internally redirect to use the "Number" controller and call the "positive" function, while calling myapp.com/1/-2 will call the "negative" function.
+ Ni.addRoute(function(path) {
+ var args = path.split('/'),
+ firstNum = parseInt(args[1]),
+ secondNum = parseInt(args[2]),
+ result = firstNum + secondNum;
+ return result > 0 ? '/Number/positive' : '/Number/negative'; // returning false would leave the path untouched
+ });
+
+You can limit the allowed HTTP methods by using custom routes.
+This will internally redirect myapp.com/comment to use your "Comments" controler and its "new" function - but only if the used HTTP Method is POST or PUT:
+
+ Ni.addRoute('/comment', '/Comments/new', ['POST', 'PUT']);
+
+And this will redirect all GET requests to myapp.com/comment to use your "Comments" controllers index function.
+ Ni.addRoute('/comment', '/Comments/', 'GET');
+
+This way you can disallow methods for some routes as well:
+ Ni.addRoute('/Comments/update', '/Home/method_not_allowed', 'GET');
+
Can I see an example?
---------------------
9 lib/ni.js
View
@@ -114,6 +114,9 @@ var Ni = function() {
fn;
parsedUrl = Ni.checkCustomRoutes(parsedUrl.pathname, req.method);
+ if (parsedUrl.indexOf('/') !== 0) {
+ parsedUrl = '/' + parsedUrl;
+ }
pathArr = parsedUrl.split('/');
args = pathArr.slice(3);
@@ -186,13 +189,13 @@ var Ni = function() {
* 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.');
+ if (arguments.length < 2 && typeof(src) !== 'function') {
+ throw new Error('Ni.addRoute expects 2 arguments if the first is not a function - '+arguments.length+' were given.');
}
var routes = Ni.config('custom_routes') || [];
routes.push({
src: src,
- dest: dest,
+ dest: dest || '/',
method: method || false
});
Ni.config('custom_routes', routes);
Please sign in to comment.
Something went wrong with that request. Please try again.