Permalink
Browse files

init version 0.1.0

  • Loading branch information...
0 parents commit 00efe57544b9ad0a6a70e741cbceb021804d8e1e @fengmk2 fengmk2 committed Jun 19, 2012
Showing with 529 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +26 −0 Makefile
  3. +108 −0 README.md
  4. +8 −0 example/connect-hello.js
  5. +38 −0 example/http-hello.js
  6. +1 −0 index.js
  7. +56 −0 lib/urlrouter.js
  8. +25 −0 package.json
  9. +2 −0 test/mocha.opts
  10. +122 −0 test/support/http.js
  11. +140 −0 test/urlrouter.js
@@ -0,0 +1,3 @@
+node_modules
+lib-cov
+coverage.html
@@ -0,0 +1,26 @@
+TESTS = test/*.js
+TESTTIMEOUT = 1000
+REPORTER = spec
+SUPPORT_VERSIONS := 1.8.0 1.8.5 1.8.6 1.8.7 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha -R $(REPORTER) --timeout $(TESTTIMEOUT) $(TESTS)
+
+test-cov: lib-cov
+ @URLROUTER_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+
+lib-cov:
+ @rm -rf ./$@
+ @jscoverage lib $@
+
+clean:
+ @rm -rf lib-cov
+ @rm -f coverage.html
+
+test-version:
+ @for version in $(SUPPORT_VERSIONS); do \
+ npm install connect@$$version; \
+ $(MAKE) test; \
+ done
+
+.PHONY: test test-cov clean lib-cov test-version
108 README.md
@@ -0,0 +1,108 @@
+# urlrouter
+
+[![Build Status](https://secure.travis-ci.org/fengmk2/urlrouter.png)](http://travis-ci.org/fengmk2/urlrouter)
+
+`http` url router.
+
+[connect](https://github.com/senchalabs/connect) missing router middleware.
+
+Support `connect` @1.8.x and @2.2.0+ .
+
+## Test connect version
+
+* 1.8.x: 1.8.0 1.8.5 1.8.6 1.8.7
+* 2.2.x: 2.2.0 2.2.1 2.2.2
+* 2.3.x: 2.3.0 2.3.1 2.3.2 2.3.3
+
+```bash
+$ make test-version
+```
+
+## Install
+
+```bash
+$ npm install urlrouter
+```
+
+## Usage
+
+### Using with `connect`
+
+```javascript
+var connect = require('connect');
+var urlrouter = require('urlrouter');
+
+connect(urlrouter(function (app) {
+ app.get('/', function (req, res, next) {
+ res.end('hello urlrouter');
+ });
+})).listen(3000);
+```
+
+### Using with `http.createServer()`
+
+```javascript
+var http = require('http');
+var urlrouter = require('urlrouter');
+
+var router = urlrouter(function (app) {
+ app.get('/', function (req, res) {
+ res.end('GET home page' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.get('/foo', function (req, res) {
+ res.end('GET ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.post('/new', function (req, res) {
+ res.write('POST ' + req.url + ' start...\n\n');
+ var counter = 0;
+ req.on('data', function (data) {
+ counter++;
+ res.write('data' + counter + ': ' + data.toString() + '\n\n');
+ });
+ req.on('end', function () {
+ res.end('POST ' + req.url + ' end.\n');
+ });
+ });
+
+ app.put('/update', function (req, res) {
+ res.end('PUT ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.delete('/remove', function (req, res) {
+ res.end('DELETE ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.options('/check', function (req, res) {
+ res.end('PUT ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+});
+
+http.createServer(router).listen(3000);
+```
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2012 fengmk2 <fengmk2@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.
@@ -0,0 +1,8 @@
+var connect = require('connect');
+var urlrouter = require('../');
+
+connect(urlrouter(function (app) {
+ app.get('/', function (req, res, next) {
+ res.end('hello urlrouter');
+ });
+})).listen(3000);
@@ -0,0 +1,38 @@
+var http = require('http');
+var urlrouter = require('../');
+
+var router = urlrouter(function (app) {
+ app.get('/', function (req, res) {
+ res.end('GET home page' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.get('/foo', function (req, res) {
+ res.end('GET ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.post('/new', function (req, res) {
+ res.write('POST ' + req.url + ' start...\n\n');
+ var counter = 0;
+ req.on('data', function (data) {
+ counter++;
+ res.write('data' + counter + ': ' + data.toString() + '\n\n');
+ });
+ req.on('end', function () {
+ res.end('POST ' + req.url + ' end.\n');
+ });
+ });
+
+ app.put('/update', function (req, res) {
+ res.end('PUT ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.delete('/remove', function (req, res) {
+ res.end('DELETE ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+
+ app.options('/check', function (req, res) {
+ res.end('PUT ' + req.url + ' , headers: ' + JSON.stringify(req.headers));
+ });
+});
+
+http.createServer(router).listen(3000);
@@ -0,0 +1 @@
+module.exports = process.env.URLROUTER_COV ? require('./lib-cov/urlrouter') : require('./lib/urlrouter');
@@ -0,0 +1,56 @@
+/*!
+ * urlrouter.js
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var urlparse = require('url').parse;
+
+var METHODS = ['get', 'post', 'put', 'delete', 'head', 'options'];
+
+function notFound(req, res) {
+ res.statusCode = 404;
+ res.end(req.method !== 'HEAD' && req.method + ' ' + req.url + ' Not Found ');
+}
+
+function router(fn) {
+ var routes = [];
+ var methods = {};
+
+ function createMethod(name) {
+ var localRoutes = routes[name.toUpperCase()] = [];
+ return function (urlroute, fn) {
+ localRoutes.push([urlroute, fn]);
+ };
+ }
+
+ METHODS.forEach(function (method) {
+ methods[method] = createMethod(method);
+ });
+
+ fn(methods);
+
+ return function lookup(req, res, next) {
+ var method = req.method.toUpperCase();
+ var localRoutes = routes[method];
+ if (localRoutes && localRoutes.length > 0) {
+ var pathname = urlparse(req.url).pathname;
+ for (var i = 0, l = localRoutes.length; i < l; i++) {
+ var route = localRoutes[i];
+ var urlroute = route[0];
+ var fn = route[1];
+ if (pathname === urlroute) {
+ return fn(req, res, next);
+ }
+ }
+ }
+ // not found
+ next ? next() : notFound(req, res);
+ };
+}
+
+module.exports = router;
@@ -0,0 +1,25 @@
+{
+ "author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)",
+ "name": "urlrouter",
+ "description": "connect missing router middleware.",
+ "version": "0.1.0",
+ "homepage": "http://github.com/fengmk2/connect-router",
+ "repository": {
+ "url": "git://github.com/fengmk2/connect-router.git"
+ },
+ "main": "index.js",
+ "scripts": {
+ "test": "make test"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*",
+ "rewire": "*",
+ "connect": ">=1.8.0"
+ },
+ "optionalDependencies": {},
+ "engines": {
+ "node": ">=0.6.7"
+ }
+}
@@ -0,0 +1,2 @@
+--require should
+--require test/support/http.js
Oops, something went wrong.

0 comments on commit 00efe57

Please sign in to comment.