Skip to content
This repository
Browse code

fixed route-separation example

  • Loading branch information...
commit f9d2432c692332077559f48f9499307a84110dd1 1 parent 6543a3d
TJ Holowaychuk authored
68 examples/route-middleware/app.js
@@ -16,44 +16,44 @@ var app = express.createServer();
16 16
17 17 // Dummy users
18 18 var users = [
19   - { id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' },
20   - { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' },
21   - { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }
  19 + { id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' }
  20 + , { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' }
  21 + , { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }
22 22 ];
23 23
24 24 function loadUser(req, res, next) {
25   - // You would fetch your user from the db
26   - var user = users[req.params.id];
27   - if (user) {
28   - req.user = user;
29   - next();
30   - } else {
31   - next(new Error('Failed to load user ' + req.params.id));
32   - }
  25 + // You would fetch your user from the db
  26 + var user = users[req.params.id];
  27 + if (user) {
  28 + req.user = user;
  29 + next();
  30 + } else {
  31 + next(new Error('Failed to load user ' + req.params.id));
  32 + }
33 33 }
34 34
35 35 function andRestrictToSelf(req, res, next) {
36   - // If our authenticated user is the user we are viewing
37   - // then everything is fine :)
38   - if (req.authenticatedUser.id == req.user.id) {
39   - next();
40   - } else {
41   - // You may want to implement specific exceptions
42   - // such as UnauthorizedError or similar so that you
43   - // can handle these in app.error() specifically
44   - // (view ./examples/pages for this)
45   - next(new Error('Unauthorized'));
46   - }
  36 + // If our authenticated user is the user we are viewing
  37 + // then everything is fine :)
  38 + if (req.authenticatedUser.id == req.user.id) {
  39 + next();
  40 + } else {
  41 + // You may want to implement specific exceptions
  42 + // such as UnauthorizedError or similar so that you
  43 + // can handle these in app.error() specifically
  44 + // (view ./examples/pages for this)
  45 + next(new Error('Unauthorized'));
  46 + }
47 47 }
48 48
49 49 function andRestrictTo(role) {
50   - return function(req, res, next) {
51   - if (req.authenticatedUser.role == role) {
52   - next();
53   - } else {
54   - next(new Error('Unauthorized'));
55   - }
  50 + return function(req, res, next) {
  51 + if (req.authenticatedUser.role == role) {
  52 + next();
  53 + } else {
  54 + next(new Error('Unauthorized'));
56 55 }
  56 + }
57 57 }
58 58
59 59 // Middleware for faux authentication
@@ -62,24 +62,24 @@ function andRestrictTo(role) {
62 62 // may interacte with middleware
63 63
64 64 app.use(function(req, res, next){
65   - req.authenticatedUser = users[0];
66   - next();
  65 + req.authenticatedUser = users[0];
  66 + next();
67 67 });
68 68
69 69 app.get('/', function(req, res){
70   - res.redirect('/user/0');
  70 + res.redirect('/user/0');
71 71 });
72 72
73 73 app.get('/user/:id', loadUser, function(req, res){
74   - res.send('Viewing user ' + req.user.name);
  74 + res.send('Viewing user ' + req.user.name);
75 75 });
76 76
77 77 app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){
78   - res.send('Editing user ' + req.user.name);
  78 + res.send('Editing user ' + req.user.name);
79 79 });
80 80
81 81 app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){
82   - res.send('Deleted user ' + req.user.name);
  82 + res.send('Deleted user ' + req.user.name);
83 83 });
84 84
85 85 app.listen(3000);
7 examples/route-separation/post.js
@@ -8,10 +8,5 @@ var posts = [
8 8 ];
9 9
10 10 exports.list = function(req, res){
11   - res.render('post/list', {
12   - locals: {
13   - title: 'Posts'
14   - , posts: posts
15   - }
16   - });
  11 + res.render('post/list', { title: 'Posts', posts: posts });
17 12 };
6 examples/route-separation/site.js
... ... @@ -1,8 +1,4 @@
1 1
2 2 exports.index = function(req, res){
3   - res.render('index', {
4   - locals: {
5   - title: 'Route Separation Example'
6   - }
7   - });
  3 + res.render('index', { title: 'Route Separation Example' });
8 4 };
25 examples/route-separation/user.js
@@ -2,17 +2,12 @@
2 2 // Fake user database
3 3
4 4 var users = [
5   - { name: 'TJ', email: 'tj@vision-media.ca' }
6   - , { name: 'Tobi', email: 'tobi@vision-media.ca' }
7   - ];
  5 + { name: 'TJ', email: 'tj@vision-media.ca' }
  6 + , { name: 'Tobi', email: 'tobi@vision-media.ca' }
  7 +];
8 8
9 9 exports.list = function(req, res){
10   - res.render('user/list', {
11   - locals: {
12   - title: 'Users'
13   - , users: users
14   - }
15   - });
  10 + res.render('user/list', { title: 'Users', users: users });
16 11 };
17 12
18 13 exports.load = function(req, res, next){
@@ -27,19 +22,15 @@ exports.load = function(req, res, next){
27 22
28 23 exports.view = function(req, res){
29 24 res.render('user/view', {
30   - locals: {
31   - title: 'Viewing user ' + req.user.name
32   - , user: req.user
33   - }
  25 + title: 'Viewing user ' + req.user.name
  26 + , user: req.user
34 27 });
35 28 };
36 29
37 30 exports.edit = function(req, res){
38 31 res.render('user/edit', {
39   - locals: {
40   - title: 'Editing user ' + req.user.name
41   - , user: req.user
42   - }
  32 + title: 'Editing user ' + req.user.name
  33 + , user: req.user
43 34 });
44 35 };
45 36

0 comments on commit f9d2432

Please sign in to comment.
Something went wrong with that request. Please try again.