Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit

  • Loading branch information...
commit c4a7f6ade40532f161a500a48d8957fff5c028af 0 parents
@baryshev authored
3  .npmignore
@@ -0,0 +1,3 @@
+*.md
+.git*
+examples/
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2012 Vadim M. Baryshev <vadimbaryshev@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 README.md
@@ -0,0 +1,10 @@
+# About
+Simple and fast router for Connect
+
+# Installation
+
+ npm install connect-route
+
+# Example
+
+See [examples](https://github.com/baryshev/connect-route/tree/master/examples) folder.
20 examples/example.js
@@ -0,0 +1,20 @@
+var
+ connectRoute = require('../index');
+ connect = require('connect'),
+ server = connect();
+
+server.use(connectRoute(function (router) {
+ router.get('/', function (req, res, next) {
+ res.end('index');
+ });
+
+ router.get('/home', function (req, res, next) {
+ res.end('home');
+ });
+
+ router.get('/home/:id', function (req, res, next) {
+ res.end('home ' + req.params.id);
+ });
+}));
+
+server.listen(3000);
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/connect-route');
108 lib/connect-route.js
@@ -0,0 +1,108 @@
+var
+ methods = [ 'get', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'copy', 'lock', 'mkcol', 'move', 'propfind', 'proppatch', 'unlock', 'report', 'mkactivity', 'checkout', 'merge' ],
+ separator = /^[\s\/]+|[\s\/]+$/g,
+ i, length,
+
+ createMethodHandler = function (method) {
+ method = method.toUpperCase();
+ return function (route, handler) {
+ this.add(method, route, handler);
+ };
+ },
+
+ Router = function () {
+ this.routes = {};
+ };
+
+Router.prototype.add = function (method, route, handler) {
+ var parts, current, i, length, name;
+
+ if (typeof handler !== 'function') { return; }
+ if (!this.routes[method]) { this.routes[method] = { childs: {}, handler: undefined, route: undefined }; }
+
+ parts = route.split('?', 1)[0].replace(separator, '').split('/');
+
+ if (!parts[0].length) {
+ this.routes[method].handler = handler;
+ this.routes[method].route = route;
+ } else {
+ current = this.routes[method];
+ for (i = 0, length = parts.length; i < length; i++) {
+ name = undefined;
+ if (parts[i].charAt(0) === ':') {
+ name = parts[i].substr(1);
+ parts[i] = '*';
+ }
+ if ((!current.childs[parts[i]] && (current.childs[parts[i]] = {})) || (parts[i] === '*' && !current.childs[parts[i]][length])) {
+ if (parts[i] === '*') {
+ current.childs[parts[i]][length] = { handler: undefined, route: undefined, childs: {}, name: name };
+ } else {
+ current.childs[parts[i]] = { handler: undefined, route: undefined, childs: {}, name: name };
+ }
+ }
+ if (parts[i] === '*') {
+ current = current.childs[parts[i]][length];
+ } else {
+ current = current.childs[parts[i]];
+ }
+ }
+ current.handler = handler;
+ current.route = route;
+ }
+};
+
+Router.prototype.match = function (method, url) {
+ var
+ parts = url.split('?', 1)[0].replace(separator, '').split('/'),
+ result = { handler: undefined, route: undefined, params: {} },
+ current, i, length;
+
+ if (this.routes[method]) {
+ if (!parts[0].length) {
+ result.handler = this.routes[method].handler;
+ result.route = this.routes[method].route;
+ } else {
+ current = this.routes[method];
+ for (i = 0, length = parts.length; i < length; i++) {
+ if (current.childs[parts[i]]) {
+ current = current.childs[parts[i]];
+ result.handler = current.handler;
+ result.route = current.route;
+ } else if (current.childs['*'] && current.childs['*'][length]) {
+ current = current.childs['*'][length];
+ result.handler = current.handler;
+ result.route = current.route;
+ result.params[current.name] = parts[i];
+ } else {
+ result.handler = undefined;
+ result.route = undefined;
+ result.params = {};
+ break;
+ }
+ }
+ }
+ }
+ return result;
+};
+
+for (i = 0, length = methods.length; i < length; i++) {
+ Router.prototype[methods[i]] = createMethodHandler(methods[i]);
+}
+
+module.exports = function (cb) {
+ var router = new Router();
+ if (typeof cb === 'function') { cb(router); }
+
+ return function (req, res, next) {
+ var action = router.match(req.method, req.url);
+ if (action.handler) {
+ req.route = action.route;
+ req.params = action.params;
+ action.handler(req, res, next);
+ } else {
+ req.route = undefined;
+ req.params = {};
+ next();
+ }
+ };
+};
13 package.json
@@ -0,0 +1,13 @@
+{
+ "name" : "connect-route",
+ "version" : "0.1.0",
+ "description" : "Simple and fast router for Connect",
+ "keywords": [ "router", "connect" ],
+ "author" : "Vadim M. Baryshev <vadimbaryshev@gmail.com>",
+ "main" : "index",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/baryshev/connect-route.git"
+ },
+ "engines" : { "node": ">= 0.4.0" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.