regex-router
Advanced tools
Comparing version 0.1.7 to 0.1.9
46
index.js
@@ -1,27 +0,29 @@ | ||
'use strict'; /*jslint node: true, indent: 2, es5: true*/ | ||
function Router() { | ||
// this.routes contents: {regex: regex, http_method: http_method, func: func} | ||
this.routes = []; | ||
'use strict'; /*jslint node: true, es5: true, indent: 2 */ | ||
function escapeRegex(string) { | ||
var specials = ['$', '(', ')', '*', '+', '.', '?', '[', '\\', ']', '^', '{', '|' , '}']; | ||
return string.replace(new RegExp('(\\' + specials.join('|\\') + ')', 'g'), '\\$1'); | ||
} | ||
var Router = module.exports = function() { | ||
this.routes = []; // e.g., [{regex: regex, http_method: http_method, func: func}, ...] | ||
this.requestListener = this.route.bind(this); | ||
}; | ||
Router.prototype.route = function(req, res) { | ||
var m; | ||
for (var i = 0, l = this.routes.length; i < l; i++) { | ||
var route = this.routes[i]; | ||
if (route.http_method === undefined || route.http_method === req.method.toLowerCase()) { | ||
m = req.url.match(route.regex); | ||
if (m) { | ||
for (var route, i = 0; (route = this.routes[i]); i++) { | ||
if (route.http_method === 'any' || route.http_method === req.method.toLowerCase()) { | ||
if ((m = req.url.match(route.regex))) | ||
return route.func(m, req, res); | ||
} | ||
} | ||
} | ||
m = req.url.match(/^(.+)$/); | ||
// check for existence of default? | ||
return this.default(m, req, res); | ||
}; | ||
Router.prototype.add = function(regex, http_method, func) { | ||
if (func === undefined) { | ||
func = http_method; | ||
http_method = undefined; | ||
} | ||
Router.prototype._add = function(regex, http_method, func) { | ||
// coerve strings to regex as exact match (must escape regex) | ||
if (!(regex instanceof RegExp)) | ||
regex = new RegExp('^' + escapeRegex(regex) + '$'); | ||
// func signature: (regex_match, req, res) | ||
@@ -31,9 +33,5 @@ this.routes.push({regex: regex, http_method: http_method, func: func}); | ||
Router.prototype.any = Router.prototype.ANY = function(url, func) { | ||
this.add(url, func); | ||
}; | ||
Router.prototype.forward = function(url, router) { | ||
this.add(url, function(m, req, res) { | ||
// oops, discard m | ||
this._add(url, 'any', function(m, req, res) { | ||
// just discard m | ||
router.route(req, res); | ||
@@ -44,8 +42,6 @@ }); | ||
// PATCH is not an official HTTP/1.1 method (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) | ||
var http_methods = ['options', 'get', 'head', 'post', 'put', 'delete', 'trace', 'connect', 'patch']; | ||
var http_methods = ['any', 'options', 'get', 'head', 'post', 'put', 'delete', 'trace', 'connect', 'patch']; | ||
http_methods.forEach(function(http_method) { | ||
Router.prototype[http_method] = Router.prototype[http_method.toUpperCase()] = | ||
function(url, func) { this.add(url, http_method, func); }; | ||
function(regex, func) { this._add(regex, http_method, func); }; | ||
}); | ||
module.exports = Router; |
{ | ||
"name": "regex-router", | ||
"version": "0.1.7", | ||
"version": "0.1.9", | ||
"description": "Route http requests via regular expressions.", | ||
@@ -19,3 +19,10 @@ "keywords": [ | ||
"license": "MIT", | ||
"author": "Christopher Brown <io@henrian.com>" | ||
"author": "Christopher Brown <io@henrian.com>", | ||
"devDependencies": { | ||
"tap": "~0.4", | ||
"request": "~2" | ||
}, | ||
"scripts": { | ||
"test": "tap test" | ||
} | ||
} |
# Regex-router | ||
Regex-router is a simple Node.js library to simplify routing. By simple, I mean 50 lines of code. | ||
Regex-router is a simple Node.js library to simplify routing. By simple, I mean 35 lines of code. | ||
@@ -5,0 +5,0 @@ # Example |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
6528
8
103
2
1
1