Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

init

  • Loading branch information...
commit a088afd75fe3a1f70f3f85a35bbc0dfb2d8cc280 0 parents
@vdemedes authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
35 Readme.md
@@ -0,0 +1,35 @@
+# Route66
+
+Route66 - is a middleware for routing for Connect 2.0. It was created, because original *connect.router* was removed from the latest version. It supports multiple methods, request params and easy patterns.
+
+# Installation
+
+`npm install route66`
+
+# Usage
+
+```coffee-script
+router = require 'route66'
+connect = require 'connect'
+app = connect()
+
+router.get '/', (req, res) -> # simplest route declaration
+ res.end()
+
+router.get '/new', (req, res) -> # another simple route
+ res.end()
+
+router.post '/', (req, res, next) -> # now, with middleware
+ do next
+, (req, res) ->
+ res.end()
+
+router.get '/:id', (req, res) -> # with request params
+ req.params.id
+ res.end()
+
+app.use router
+app.listen 3000
+```
+
+Available methods: get, post, put, patch, delete, head.
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/route66');
52 lib/route66.coffee
@@ -0,0 +1,52 @@
+async = require 'async'
+
+Route66 = (req, res, next) ->
+ for route in routes[req.method.toLowerCase()]
+ if route.match.test req.url
+ values = route.match.exec(req.url).slice 1
+ i = 0
+ req.params = {}
+ loop
+ break if i >= values.length
+ req.params[route.params[i]] = values[i]
+ i++
+ return async.forEachSeries route.functions, (fn, nextFn) ->
+ fn(req, res, nextFn)
+ , ->
+ do next
+
+Route66.addRoute = (method, match, functions) ->
+ params = []
+ matchClone = match
+ loop
+ result = /\:([A-Za-z_]+)\/?/.exec matchClone
+ if result
+ params.push result.slice(1).toString()
+ matchClone = matchClone.replace /\:([A-Za-z_]+)\/?/, ''
+ break if not /\:([A-Za-z_]+)\/?/.test matchClone
+ routes[method].push
+ match: new RegExp match.replace(/\//g, '\\/?').replace(/\:([A-Za-z_]+)(\?)?\/?/g, '([A-Za-z0-9_]+)$2')
+ params: params
+ functions: toArray(functions).slice 1
+ do Route66.sort
+
+toArray = (object) ->
+ items = []
+ for item of object
+ items.push object[item]
+ items
+
+routes = {}
+methods = ['get', 'post', 'patch', 'put', 'delete', 'head']
+
+for method in methods
+ routes[method] = []
+ Route66[method] = (match) ->
+ Route66.addRoute method, match, arguments
+
+Route66.sort = ->
+ for method in methods
+ routes[method].sort (a, b) ->
+ b.match.toString().length - a.match.toString().length
+
+module.exports = Route66
81 lib/route66.js
@@ -0,0 +1,81 @@
+var Route66, async, method, methods, routes, toArray, _i, _len;
+
+async = require('async');
+
+Route66 = function(req, res, next) {
+ var i, route, values, _i, _len, _ref;
+ _ref = routes[req.method.toLowerCase()];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ route = _ref[_i];
+ if (route.match.test(req.url)) {
+ values = route.match.exec(req.url).slice(1);
+ i = 0;
+ req.params = {};
+ while (true) {
+ if (i >= values.length) break;
+ req.params[route.params[i]] = values[i];
+ i++;
+ }
+ return async.forEachSeries(route.functions, function(fn, nextFn) {
+ return fn(req, res, nextFn);
+ }, function() {
+ return next();
+ });
+ }
+ }
+};
+
+Route66.addRoute = function(method, match, functions) {
+ var matchClone, params, result;
+ params = [];
+ matchClone = match;
+ while (true) {
+ result = /\:([A-Za-z_]+)\/?/.exec(matchClone);
+ if (result) {
+ params.push(result.slice(1).toString());
+ matchClone = matchClone.replace(/\:([A-Za-z_]+)\/?/, '');
+ }
+ if (!/\:([A-Za-z_]+)\/?/.test(matchClone)) break;
+ }
+ routes[method].push({
+ match: new RegExp(match.replace(/\//g, '\\/?').replace(/\:([A-Za-z_]+)(\?)?\/?/g, '([A-Za-z0-9_]+)$2')),
+ params: params,
+ functions: toArray(functions).slice(1)
+ });
+ return Route66.sort();
+};
+
+toArray = function(object) {
+ var item, items;
+ items = [];
+ for (item in object) {
+ items.push(object[item]);
+ }
+ return items;
+};
+
+routes = {};
+
+methods = ['get', 'post', 'patch', 'put', 'delete', 'head'];
+
+for (_i = 0, _len = methods.length; _i < _len; _i++) {
+ method = methods[_i];
+ routes[method] = [];
+ Route66[method] = function(match) {
+ return Route66.addRoute(method, match, arguments);
+ };
+}
+
+Route66.sort = function() {
+ var method, _j, _len2, _results;
+ _results = [];
+ for (_j = 0, _len2 = methods.length; _j < _len2; _j++) {
+ method = methods[_j];
+ _results.push(routes[method].sort(function(a, b) {
+ return b.match.toString().length - a.match.toString().length;
+ }));
+ }
+ return _results;
+};
+
+module.exports = Route66;
22 package.json
@@ -0,0 +1,22 @@
+{
+ "name": "route66",
+ "version": "0.1.0",
+ "author": "Vadim Demedes <sbioko@gmail.com>",
+ "description": "Routing functionality/middleware for Connect 2.0",
+ "main": "./index.js",
+ "keywords": ["route", "connect", "routes", "routing"],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/vdemedes/route66.git"
+ },
+ "license": "MIT",
+ "engine": {
+ "node": ">=0.4"
+ },
+ "devDependencies": {
+ "connect": "2.0.2"
+ },
+ "dependencies": {
+ "async": "*"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.