Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

regex-router

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

regex-router - npm Package Compare versions

Comparing version 0.1.7 to 0.1.9

.travis.yml

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc