Permalink
Browse files

fixed route-separation example

  • Loading branch information...
1 parent 6543a3d commit f9d2432c692332077559f48f9499307a84110dd1 @tj tj committed Nov 25, 2010
@@ -16,44 +16,44 @@ var app = express.createServer();
// Dummy users
var users = [
- { id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' },
- { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' },
- { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }
+ { id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' }
+ , { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' }
+ , { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }
];
function loadUser(req, res, next) {
- // You would fetch your user from the db
- var user = users[req.params.id];
- if (user) {
- req.user = user;
- next();
- } else {
- next(new Error('Failed to load user ' + req.params.id));
- }
+ // You would fetch your user from the db
+ var user = users[req.params.id];
+ if (user) {
+ req.user = user;
+ next();
+ } else {
+ next(new Error('Failed to load user ' + req.params.id));
+ }
}
function andRestrictToSelf(req, res, next) {
- // If our authenticated user is the user we are viewing
- // then everything is fine :)
- if (req.authenticatedUser.id == req.user.id) {
- next();
- } else {
- // You may want to implement specific exceptions
- // such as UnauthorizedError or similar so that you
- // can handle these in app.error() specifically
- // (view ./examples/pages for this)
- next(new Error('Unauthorized'));
- }
+ // If our authenticated user is the user we are viewing
+ // then everything is fine :)
+ if (req.authenticatedUser.id == req.user.id) {
+ next();
+ } else {
+ // You may want to implement specific exceptions
+ // such as UnauthorizedError or similar so that you
+ // can handle these in app.error() specifically
+ // (view ./examples/pages for this)
+ next(new Error('Unauthorized'));
+ }
}
function andRestrictTo(role) {
- return function(req, res, next) {
- if (req.authenticatedUser.role == role) {
- next();
- } else {
- next(new Error('Unauthorized'));
- }
+ return function(req, res, next) {
+ if (req.authenticatedUser.role == role) {
+ next();
+ } else {
+ next(new Error('Unauthorized'));
}
+ }
}
// Middleware for faux authentication
@@ -62,24 +62,24 @@ function andRestrictTo(role) {
// may interacte with middleware
app.use(function(req, res, next){
- req.authenticatedUser = users[0];
- next();
+ req.authenticatedUser = users[0];
+ next();
});
app.get('/', function(req, res){
- res.redirect('/user/0');
+ res.redirect('/user/0');
});
app.get('/user/:id', loadUser, function(req, res){
- res.send('Viewing user ' + req.user.name);
+ res.send('Viewing user ' + req.user.name);
});
app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){
- res.send('Editing user ' + req.user.name);
+ res.send('Editing user ' + req.user.name);
});
app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){
- res.send('Deleted user ' + req.user.name);
+ res.send('Deleted user ' + req.user.name);
});
app.listen(3000);
@@ -8,10 +8,5 @@ var posts = [
];
exports.list = function(req, res){
- res.render('post/list', {
- locals: {
- title: 'Posts'
- , posts: posts
- }
- });
+ res.render('post/list', { title: 'Posts', posts: posts });
};
@@ -1,8 +1,4 @@
exports.index = function(req, res){
- res.render('index', {
- locals: {
- title: 'Route Separation Example'
- }
- });
+ res.render('index', { title: 'Route Separation Example' });
};
@@ -2,17 +2,12 @@
// Fake user database
var users = [
- { name: 'TJ', email: 'tj@vision-media.ca' }
- , { name: 'Tobi', email: 'tobi@vision-media.ca' }
- ];
+ { name: 'TJ', email: 'tj@vision-media.ca' }
+ , { name: 'Tobi', email: 'tobi@vision-media.ca' }
+];
exports.list = function(req, res){
- res.render('user/list', {
- locals: {
- title: 'Users'
- , users: users
- }
- });
+ res.render('user/list', { title: 'Users', users: users });
};
exports.load = function(req, res, next){
@@ -27,19 +22,15 @@ exports.load = function(req, res, next){
exports.view = function(req, res){
res.render('user/view', {
- locals: {
- title: 'Viewing user ' + req.user.name
- , user: req.user
- }
+ title: 'Viewing user ' + req.user.name
+ , user: req.user
});
};
exports.edit = function(req, res){
res.render('user/edit', {
- locals: {
- title: 'Editing user ' + req.user.name
- , user: req.user
- }
+ title: 'Editing user ' + req.user.name
+ , user: req.user
});
};

0 comments on commit f9d2432

Please sign in to comment.