Permalink
Browse files

Added route-map example

  • Loading branch information...
tj committed Jun 29, 2012
1 parent bddcdee commit 39efa452fcb075a9a5870f1f71fe60a8742e687b
Showing with 62 additions and 0 deletions.
  1. +62 −0 examples/route-map/index.js
@@ -0,0 +1,62 @@
+
+var express = require('../../lib/express')
+ , app = express();
+
+app.map = function(a, route){
+ route = route || '';
+ for (var key in a) {
+ switch (typeof a[key]) {
+ // { '/path': { ... }}
+ case 'object':
+ app.map(a[key], route + key);
+ break;
+ // get: function(){ ... }
+ case 'function':
+ console.log('%s %s', key, route);
+ app[key](route, a[key]);
+ break;
+ }
+ }
+};
+
+var users = {
+ list: function(req, res){
+ res.send('user list');
+ },
+
+ get: function(req, res){
+ res.send('user ' + req.params.uid);
+ },
+
+ del: function(req, res){
+ res.send('delete users');
+ }
+};
+
+var pets = {
+ list: function(req, res){
+ res.send('user ' + req.params.uid + '\'s pets');
+ },
+
+ del: function(req, res){
+ res.send('delete ' + req.params.uid + '\'s pet ' + req.params.pid);
+ }
+};
+
+app.map({
+ '/users': {
+ get: users.list,
+ del: users.del,
+ '/:uid': {
+ get: users.get,
+ '/pets': {
+ get: pets.list,
+ '/:pid': {
+ del: pets.del
+ }
+ }
+ }
+ }
+});
+
+app.listen(3000);

0 comments on commit 39efa45

Please sign in to comment.