app.js
This file
Path
examples/resource/app.js
74 lines · 1.8 KiB · JavaScript
Last changed by
Updated examplesTj Holowaychukbefore adoptionf60bcca
History of this fileTake it
Added resource exampleTj Holowaychukbefore adoption6a10a67
lines 1–13why, one step back12/**3 * Module dependencies.4 */56var express = require('../../lib/express');78var app = express.createServer();910// Ad-hoc example resource method1112app.resource = function(path, obj) {13 this.get(path, obj.index);Updated examplesTj Holowaychukbefore adoptionf60bcca
lines 14–16why, one step back14 this.get(path + '/:a..:b', function(req, res){15 var a = parseInt(req.params.a, 10),16 b = parseInt(req.params.b, 10);Added range example to resourceTj Holowaychukbefore adoption5bc9866
lines 17–18why, one step back17 obj.range(req, res, a, b);18 });Added resource exampleTj Holowaychukbefore adoption6a10a67
lines 19–27why, one step back19 this.get(path + '/:id', obj.show);20 this.del(path + '/:id', obj.destroy);21};2223// Fake records2425var users = [26 { name: 'tj' },27 { name: 'ciaran' },Added / to resource exampleTj Holowaychukbefore adoption61d82e2
lines 28–31why, one step back28 { name: 'aaron' },29 { name: 'guillermo' },30 { name: 'simon' },31 { name: 'tobi' }Added resource exampleTj Holowaychukbefore adoption6a10a67
lines 32–39why, one step back32];3334// Fake model3536var User = {37 index: function(req, res){38 res.send(users);39 },Updated examplesTj Holowaychukbefore adoptionf60bcca
lines 40–41why, one step back40 show: function(req, res){41 res.send(users[req.params.id] || { error: 'Cannot find user' });Added resource exampleTj Holowaychukbefore adoption6a10a67
line 42why, one step back42 },Updated examplesTj Holowaychukbefore adoptionf60bcca
lines 43–46why, one step back43 destroy: function(req, res){44 var id = req.params.id;45 var destroyed = id in users;46 delete users[id];Added resource exampleTj Holowaychukbefore adoption6a10a67
line 47why, one step back47 res.send(destroyed ? 'destroyed' : 'Cannot find user');Added range example to resourceTj Holowaychukbefore adoption5bc9866
lines 48–50why, one step back48 },49 range: function(req, res, a, b){50 res.send(users.slice(a, b));Added resource exampleTj Holowaychukbefore adoption6a10a67
lines 51–56why, one step back51 }52};5354// curl http://localhost:3000/users -- responds with all users55// curl http://localhost:3000/users/1 -- responds with user 156// curl http://localhost:3000/users/4 -- responds with errorAdded range example to resourceTj Holowaychukbefore adoption5bc9866
line 57why, one step back57// curl http://localhost:3000/users/1..3 -- responds with several usersAdded resource exampleTj Holowaychukbefore adoption6a10a67
lines 58–60why, one step back58// curl -X DELETE http://localhost:3000/users/1 -- deletes the user5960app.resource('/users', User);Added / to resource exampleTj Holowaychukbefore adoption61d82e2
lines 61–66why, one step back6162app.get('/', function(req, res){63 res.send([64 '<h1>Examples:</h1> <ul>',65 '<li>GET /users</li>',66 '<li>GET /users/1</li>',Added range example to resourceTj Holowaychukbefore adoption5bc9866
lines 67–68why, one step back67 '<li>GET /users/3</li>',68 '<li>GET /users/1..3</li>',Added / to resource exampleTj Holowaychukbefore adoption61d82e2
lines 69–72why, one step back69 '<li>DELETE /users/4</li>',70 '</ul>',71 ].join('\n')); 72});Added resource exampleTj Holowaychukbefore adoption6a10a67
lines 73–74why, one step back7374app.listen(3000);