can-route
Advanced tools
Comparing version 0.0.4 to 0.1.0
43
index.js
var url = require('url') | ||
var named = require('named-regexp').named | ||
var collapse = require('collapse-array') | ||
var methods = require('./lib/methods') | ||
var all = require('methods') | ||
var methods = require('./lib/methods')(all) | ||
var shared = require('./shared') | ||
var live = function(route) { | ||
var format = /^\/(.+)\/([mig]*)$/ | ||
var regexp = (route.match(format) || []) | ||
.slice(1) | ||
return named(RegExp.apply(this, regexp)) | ||
} | ||
var Can = function() { | ||
if (!(this instanceof Can)) { | ||
return new Can | ||
} | ||
this.routes = {} | ||
} | ||
methods.call(Can.prototype) | ||
Can.prototype.route = function(req, res) { | ||
var method = req.method.toLowerCase() | ||
var routes = this.routes | ||
var routeable = false | ||
for (var route in routes) { | ||
routeable = live(route).exec( | ||
url.parse(req.url).pathname | ||
) | ||
if (routeable && routes[route][method]) { | ||
var params = collapse(routeable.captures) | ||
routes[route][method](req, res, params) | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
module.exports = Can | ||
module.exports = shared(methods, url) |
@@ -1,3 +0,1 @@ | ||
var methods = require('methods') | ||
var append = function(route, method, handler) { | ||
@@ -19,5 +17,5 @@ var routes = this.routes | ||
module.exports = (function() { | ||
module.exports = function(methods) { | ||
return function() { | ||
methods.forEach(function(method) { | ||
methods.forEach(function(method) { | ||
this[method.toUpperCase()] = | ||
@@ -29,2 +27,2 @@ this[method] = function(route, handler) { | ||
} | ||
})() | ||
} |
{ | ||
"name": "can-route", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "A simple regexp router that runs inside a `http.createServer` handler and returns false when it can’t route a request.", | ||
@@ -18,4 +18,16 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
"test": "tape test/server.js" | ||
}, | ||
"testling": { | ||
"files": "test/browser.js", | ||
"browsers": { | ||
"ie": [7, 8, 9, 10], | ||
"chrome": [25], | ||
"firefox": [4, 19], | ||
"safari": [5.1, 6], | ||
"opera": [10, 12], | ||
"iphone": [6], | ||
"android": [4.2] | ||
} | ||
}, | ||
"repository": { | ||
@@ -22,0 +34,0 @@ "type": "git", |
@@ -6,2 +6,3 @@ # can-route | ||
[![Browser support](https://ci.testling.com/michaelrhodes/can-route.png)](https://ci.testling.com/michaelrhodes/can-route) | ||
@@ -41,3 +42,36 @@ ## Install | ||
#### Browser | ||
can-route also works in the browser, with basically the same API. The difference is that only the get method may be used to attach handlers, and | ||
``` js | ||
var can = require('can-route')() | ||
can.get(/^\/$/, function() { | ||
// No arguments for routes without parameters. | ||
}) | ||
can.get(/^\/(:<name>[a-z]+)\/?$/i, function(params) { | ||
// The only possible argument is the param object. | ||
}) | ||
can.get(/#\/(:<name>[a-z]+)\/$/i, function(params) { | ||
// It's possible to define routes based on | ||
// the value of location.hash. | ||
}) | ||
window.onpopstate = function() { | ||
if (!can.route(window.location)) { | ||
// Handle 404 | ||
} | ||
} | ||
window.onhashchange = function() { | ||
var includeQueryAndHash = true | ||
if (!can.route(window.location, includeQueryAndHash)) { | ||
// Handle 404 | ||
} | ||
} | ||
``` | ||
### License | ||
[MIT](http://opensource.org/licenses/MIT) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
8803
9
235
76
2