-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters