Permalink
Browse files

Added README doc

  • Loading branch information...
1 parent d9bcc7d commit c87f03c11748e688744212184df3d997c3e072f2 @tarruda committed Aug 29, 2012
Showing with 106 additions and 25 deletions.
  1. +1 −1 Makefile
  2. +75 −0 README.mkd
  3. +2 −0 package.json
  4. +1 −0 src/router.coffee
  5. +27 −24 test/router.coffee
View
@@ -4,7 +4,7 @@ test:
@./node_modules/.bin/mocha --require should --compilers coffee:coffee-script $(TESTS)
test-docs:
- @./node_modules/.bin/mocha --reporter doc --require should --compilers coffee:coffee-script $(TESTS) > tests.html
+ @./node_modules/.bin/mocha --reporter markdown --require should --compilers coffee:coffee-script $(TESTS) > doc.html
compile:
@./node_modules/.bin/coffee -o lib src
View
@@ -0,0 +1,75 @@
+## flask-router
+
+ Routing system for node.js/connect based on Flask(http://flask.pocoo.org/).
+
+#### Usage
+
+```js
+var connect = require('connect')
+ , app = connect()
+ , router = require('flask-router')();
+
+app.use(router.route);
+
+router.get('/users/<str(max=5,min=2):id>', function (req, res) {
+ console.log(req.params.id);
+ res.end();
+});
+
+router.post('/users/<str(len=7):id>', function (req, res) {
+ console.log(req.params.id);
+ res.end();
+});
+
+router.put('/customers/<id>', function (req, res) {
+ console.log(req.params.id);
+ res.end();
+});
+```
+
+ Can assign multiple handler functions to the same rule:
+
+```js
+router.get('/pattern/that/uses/many/handlers'
+, function(req, res, next) {
+ res.write('part1');
+ return next();
+}, function(req, res, next) {
+ res.write('part2');
+ return next();
+});
+
+router.get('/pattern/that/uses/many/handlers', function(req, res) {
+ res.write('part3');
+ return res.end();
+});
+// All three handlers will be executed when the url match, so the final
+// response will be 'part1part2part3'
+```
+
+ Custom parameter parsers can be registered(these are known as 'converters'
+ in Flask/Werkzeug):
+
+```js
+router.registerParser('options', function(str) {
+ var rv = {};
+ , options = str.split('/')
+ , i, len, kv, key, value;
+ for (i = 0, len = options.length; i < len; i++) {
+ option = options[i];
+ kv = option.split('=');
+ key = kv[0], value = kv[1];
+ rv[key] = value;
+ }
+ return rv;
+});
+
+router.get('/queryable/<options:query>', function(req, res) {
+ console.log(JSON.stringify(req.params.query));
+ res.end();
+});
+// If '/queryable/gt=5/lt=10/limit=20' was requested,
+// the output would be {"limit":"20","gt":"5","lt":"10"}
+```
+
+ See tests for more examples.
View
@@ -2,6 +2,7 @@
"name": "flask-router",
"version": "0.0.1",
"description": "Flask-inspired routing system for node and connect.\nNice if you just need a routing system without depending on connect, or need routing middleware without all features provided by express.",
+ "main": "./lib/router.js",
"repository": {
"type": "git",
"url": ""
@@ -20,6 +21,7 @@
"license": "BSD",
"devDependencies": {
"coffee-script": "1.3.3",
+ "connect": "*",
"should": "*",
"mocha": "*"
},
View
@@ -314,6 +314,7 @@ module.exports = (parsers) ->
return {
route: (req, res, next) -> r.route(req, res, next)
+ registerParser: (name, parser) -> compiler.parsers[name] = parser
get: (pattern, handlers...) -> r.register('GET', pattern, handlers...)
post: (pattern, handlers...) -> r.register('POST', pattern, handlers...)
put: (pattern, handlers...) -> r.register('PUT', pattern, handlers...)
View
@@ -6,33 +6,35 @@ describe 'Rules', ->
app = connect()
app.use(router.route)
- router.get '/$imple/.get/pattern$', (req, res) ->
- res.write('body1')
- res.end()
+ before ->
+ router.get '/$imple/.get/pattern$', (req, res) ->
+ res.write('body1')
+ res.end()
- router.post('/not-a-get/pattern*', -> res.end())
+ router.post('/not-a-get/pattern*', -> res.end())
- router.del('/not-a-get/pattern*', -> res.end())
+ router.del('/not-a-get/pattern*', -> res.end())
- router.get '/^pattern/that/uses/many/handlers',
- (req, res, next) -> res.write('part1'); next(),
- (req, res, next) -> res.write('part2'); next()
+ router.get '/^pattern/that/uses/many/handlers',
+ (req, res, next) -> res.write('part1'); next(),
+ (req, res, next) -> res.write('part2'); next()
- router.get '/^pattern/that/uses/many/handlers',
- (req, res) -> res.write('part3'); res.end()
+ router.get '/^pattern/that/uses/many/handlers',
+ (req, res) -> res.write('part3'); res.end()
- router.get '/cancel',
- (req, res, next) -> res.write('p1'); next(),
- (req, res, next) -> res.write('p2'); res.end(),
- (req, res, next) -> res.write('p3'); next(),
- (req, res, next) -> res.write('p4'); res.end()
+ router.get '/cancel',
+ (req, res, next) -> res.write('p1'); next(),
+ (req, res, next) -> res.write('p2'); res.end(),
+ (req, res, next) -> res.write('p3'); next(),
+ (req, res, next) -> res.write('p4'); res.end()
it 'should match simple patterns', (done) ->
app.request()
.get('/$imple/.get/pattern$')
.end (res) ->
res.body.should.eql('body1')
done()
+ return
it "should return 405 when pattern doesn't match method", (done) ->
app.request()
@@ -285,18 +287,18 @@ describe 'Multiple parameters', ->
describe 'Custom parser', ->
- router = createRouter
- options: (str) ->
- rv = {}
- options = str.split('/')
- for option in options
- [key, value] = option.split('=')
- rv[key] = value
- return rv
-
+ router = createRouter()
app = connect()
app.use(router.route)
+ router.registerParser 'options', (str) ->
+ rv = {}
+ options = str.split('/')
+ for option in options
+ [key, value] = option.split('=')
+ rv[key] = value
+ return rv
+
router.get '/transactions/<options:query>', (req, res) ->
res.write(JSON.stringify(req.params.query))
res.end()
@@ -305,6 +307,7 @@ describe 'Custom parser', ->
app.request()
.get('/transactions/gt=5/lt=10/limit=20')
.end (res) ->
+ console.log(res.body)
JSON.parse(res.body).should.eql
gt: '5'
lt: '10'

0 comments on commit c87f03c

Please sign in to comment.