can-route
Advanced tools
Comparing version 0.2.5 to 0.3.0
{ | ||
"name": "can-route", | ||
"version": "0.2.5", | ||
"description": "A simple regexp router that runs inside a `http.createServer` handler and returns false when it can’t route a request.", | ||
"version": "0.3.0", | ||
"description": "A simple regexp router that returns false when it can’t handle a request.", | ||
"main": "index.js", | ||
@@ -11,4 +11,4 @@ "browser": "browser.js", | ||
"dependencies": { | ||
"methods": "~0.0.1", | ||
"collapse-array": "~0.0.1", | ||
"methods": "~0.1.0", | ||
"collapse-array": "~1.0.1", | ||
"named-regexp": "~0.1.1" | ||
@@ -27,3 +27,3 @@ }, | ||
"chrome": [20, 25, 29], | ||
"firefox": [4, 7, 19, 24], | ||
"firefox": [3, 4, 7, 19, 24], | ||
"safari": [5.1, 6], | ||
@@ -30,0 +30,0 @@ "opera": [10, 12, 15], |
# can-route | ||
can-route is a simple router that runs inside a `http.createServer` handler and returns false when it can’t route a request. Its routes are defined with [named regular expressions](https://npm.im/named-regexp). | ||
can-route is a simple router that returns false when it can’t handle a request. Its routes are defined with [named regular expressions](https://npm.im/named-regexp). | ||
@@ -32,5 +32,15 @@ [![Build status](https://travis-ci.org/michaelrhodes/can-route.png?branch=master)](https://travis-ci.org/michaelrhodes/can-route) | ||
// Server | ||
http.createServer(function(req, res) { | ||
if (!can.route(req, res)) { | ||
// No route has not been registered for | ||
// this URI + HTTP method. | ||
if (!can.route(req, res)) { | ||
// A route *has* been registered for this | ||
// URI, but not for this HTTP method. | ||
if (can.match(req, res)) { | ||
res.statusCode = 405 | ||
res.end() | ||
return | ||
} | ||
res.statusCode = 404 | ||
@@ -37,0 +47,0 @@ res.end() |
var named = require('named-regexp').named | ||
var collapse = require('collapse-array') | ||
var live = function(route) { | ||
var regexp = function(route) { | ||
var format = /^\/(.+)\/([mig]*)$/ | ||
@@ -15,14 +15,4 @@ var regexp = (route.match(format) || []) | ||
var Can = function() { | ||
if (!(this instanceof Can)) { | ||
return new Can | ||
} | ||
} | ||
methods.call(Can.prototype) | ||
Can.prototype.routes = {} | ||
Can.prototype.route = function(req, res) { | ||
var hash = !server && res === true | ||
var getPathname = function(req, res) { | ||
var hash = !server && res === true | ||
var pathname = (typeof req == 'string' ? | ||
@@ -32,5 +22,2 @@ (!hash ? req.replace(/(\?|#).+$/, '') : req) : | ||
) | ||
var method = (server ? | ||
req.method.toLowerCase() : 'get' | ||
) | ||
@@ -50,16 +37,49 @@ if (!pathname) { | ||
var routes = this.routes | ||
var routeable = false | ||
for (var route in routes) { | ||
routeable = live(route).exec(pathname) | ||
if (routeable && routes[route][method]) { | ||
var params = collapse(routeable.captures) | ||
var args = (server ? | ||
[req, res, params] : [params] | ||
) | ||
routes[route][method].apply(this, args) | ||
return true | ||
} | ||
return pathname | ||
} | ||
var Can = function() { | ||
if (!(this instanceof Can)) { | ||
return new Can | ||
} | ||
} | ||
methods.call(Can.prototype) | ||
Can.prototype.routes = {} | ||
Can.prototype.match = function(req, res) { | ||
var pathname = getPathname(req, res) | ||
var pattern = null | ||
for (var route in this.routes) { | ||
pattern = regexp(route) | ||
if (pattern.test(pathname)) { | ||
return { | ||
route: route, | ||
captures: pattern.exec(pathname).captures | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
Can.prototype.route = function(req, res) { | ||
var method = (server ? req.method : 'GET').toLowerCase() | ||
var matched = this.match(req, res) | ||
if (!matched) { | ||
return false | ||
} | ||
var route = this.routes[matched.route][method] | ||
if (route) { | ||
var params = collapse(matched.captures) | ||
var args = server ? [req, res, params] : [params] | ||
route.apply(this, args) | ||
return true | ||
} | ||
return false | ||
@@ -66,0 +86,0 @@ } |
@@ -7,8 +7,6 @@ var shite = !('bind' in Function && 'onhashchange' in window) | ||
} | ||
this.hash = window.location.hash | ||
this.handler = handler | ||
if (!shite) { | ||
this.handler = this.handler.bind(this) | ||
} | ||
this.handler = (!shite ? | ||
handler.bind(this) : handler | ||
) | ||
this.start() | ||
@@ -15,0 +13,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10349
294
87
+ Addedcollapse-array@1.0.1(transitive)
+ Addedmethods@0.1.0(transitive)
- Removedcollapse-array@0.0.1(transitive)
- Removedmethods@0.0.1(transitive)
Updatedcollapse-array@~1.0.1
Updatedmethods@~0.1.0