Fixed resource example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 6f2120b · Browse files · 1 file·+39 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+39 added−39 removed
- other:+39 added−39 removed
Provenance
- Commit
a779369- Tree
c2532e1- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
6f2120b
1 file changed+39 added−39 removed
modified:examples/resource/app.js+39 added-39 removed
8 unmodified lines9101112131415161718192013141516171819202122232425262728293031262728293031323334343536373839404142434445464748495051373839404142434445464748495051525354553 unmodified lines59606162636465666768697071636465666768697071727374758 unmodified lines
// Ad-hoc example resource method
app.resource = function(path, obj) { this.get(path, obj.index); this.get(path + '/:a..:b', function(req, res){ var a = parseInt(req.params.a, 10), b = parseInt(req.params.b, 10); obj.range(req, res, a, b); }); this.get(path + '/:id', obj.show); this.del(path + '/:id', obj.destroy); this.get(path, obj.index); this.get(path + '/:a..:b', function(req, res){ var a = parseInt(req.params.a, 10) , b = parseInt(req.params.b, 10); obj.range(req, res, a, b); }); this.get(path + '/:id', obj.show); this.del(path + '/:id', obj.destroy);};
// Fake records
var users = [ { name: 'tj' }, { name: 'ciaran' }, { name: 'aaron' }, { name: 'guillermo' }, { name: 'simon' }, { name: 'tobi' } { name: 'tj' } , { name: 'ciaran' } , { name: 'aaron' } , { name: 'guillermo' } , { name: 'simon' } , { name: 'tobi' }];
// Fake controller// Fake mode.
var User = { index: function(req, res){ res.send(users); }, show: function(req, res){ res.send(users[req.params.id] || { error: 'Cannot find user' }); }, destroy: function(req, res){ var id = req.params.id; var destroyed = id in users; delete users[id]; res.send(destroyed ? 'destroyed' : 'Cannot find user'); }, range: function(req, res, a, b){ res.send(users.slice(a, b+1)); } index: function(req, res){ res.send(users); }, show: function(req, res){ res.send(users[req.params.id] || { error: 'Cannot find user' }); }, destroy: function(req, res){ var id = req.params.id; var destroyed = id in users; delete users[id]; res.send(destroyed ? 'destroyed' : 'Cannot find user'); }, range: function(req, res, a, b){ res.send(users.slice(a, b+1)); }};
// curl http://localhost:3000/users -- responds with all users// curl http://localhost:3000/users/1 -- responds with user 13 unmodified lines
app.resource('/users', User);
app.get('/', function(req, res){ res.send([ '<h1>Examples:</h1> <ul>', '<li>GET /users</li>', '<li>GET /users/1</li>', '<li>GET /users/3</li>', '<li>GET /users/1..3</li>', '<li>DELETE /users/4</li>', '</ul>', ].join('\n')); res.send([ '<h1>Examples:</h1> <ul>' , '<li>GET /users</li>' , '<li>GET /users/1</li>' , '<li>GET /users/3</li>' , '<li>GET /users/1..3</li>' , '<li>DELETE /users/4</li>' , '</ul>' ].join('\n')); });
app.listen(3000);console.log('Express app started on port 3000');No newline at end of fileNo newline at end of file