Added range example to resource

This commit is contained in:
Tj Holowaychuk 2010-07-23 16:57:12 -07:00
parent 61d82e210a
commit 5bc9866f43

View File

@ -11,6 +11,11 @@ var app = express.createServer();
app.resource = function(path, obj) {
this.get(path, obj.index);
this.get(path + '/:a..:b', function(req, res, params){
var a = parseInt(params.a, 10),
b = parseInt(params.b, 10);
obj.range(req, res, a, b);
});
this.get(path + '/:id', obj.show);
this.del(path + '/:id', obj.destroy);
};
@ -39,12 +44,16 @@ var User = {
var destroyed = params.id in users;
delete users[params.id];
res.send(destroyed ? 'destroyed' : 'Cannot find user');
},
range: function(req, res, a, b){
res.send(users.slice(a, b));
}
};
// curl http://localhost:3000/users -- responds with all users
// curl http://localhost:3000/users/1 -- responds with user 1
// curl http://localhost:3000/users/4 -- responds with error
// curl http://localhost:3000/users/1..3 -- responds with several users
// curl -X DELETE http://localhost:3000/users/1 -- deletes the user
app.resource('/users', User);
@ -54,7 +63,8 @@ app.get('/', function(req, res){
'<h1>Examples:</h1> <ul>',
'<li>GET /users</li>',
'<li>GET /users/1</li>',
'<li>GET /users/4</li>',
'<li>GET /users/3</li>',
'<li>GET /users/1..3</li>',
'<li>DELETE /users/4</li>',
'</ul>',
].join('\n'));