Created
February 25, 2012 21:12
-
-
Save Aaronontheweb/1910743 to your computer and use it in GitHub Desktop.
How to structure an Express Controller Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Module dependencies | |
*/ | |
var Post = require('../models/post') | |
, PostValidator = require('../validators/post-validator') | |
, requiresLogin = require('../helpers/requireLogin').requiresLogin | |
, marked = require('marked'); | |
module.exports = function(app, repository){ | |
//Instantiate a new validator | |
var validator = new PostValidator(repository); | |
/* Params */ | |
app.param('slug', function(req, res, next, id){ | |
repository.get(id, function(error, post){ | |
if(error) return next(error); | |
if(!post) return next(new Error('Unable to find post '+ id)); | |
post.body = marked(post.body); | |
req.post = post; | |
next(); | |
}); | |
}); | |
/* Routes */ | |
app.get('/post', function(req, res){ | |
res.redirect('/'); | |
}); | |
app.get('/post/new', requiresLogin, function(req, res){ | |
res.render('post/new', {title: 'Add Post'}); | |
}); | |
app.post('/post/new', requiresLogin,function(req, res){ | |
var post = req.body.post; | |
post.username = req.user.id; | |
validator.validateForm(req.body.post, function(error){ | |
if(error) return res.render('post/new', {title: 'Add Post', errors:error}); | |
var fullPost = new Post(post.title, post.body, post.username); | |
repository.save(fullPost, function(error, data){ | |
if(error) return res.render('post/new', {title: 'Add Post', errors:[error]}); | |
res.redirect('/post/'+ data.id); | |
}); | |
}); | |
}); | |
/* | |
* GET {root}/{id of a post} | |
* e.g. www.site.com/my-first-post | |
*/ | |
app.get('/post/:slug', function(req, res){ | |
res.render('post/view', {title:req.post.title, post:req.post}); | |
}); | |
app.get('/post/:slug/edit', requiresLogin, function(req,res){ | |
}); | |
app.post('/post/:slug/edit', requiresLogin, function(req,res){ | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, UserRepository = require('./models/dummy/userRepository') | |
, PostRepository = require('./models/dummy/postRepository') | |
, everyauth = require('everyauth'); | |
var app = module.exports = express.createServer(); | |
var userRepo = new UserRepository(); | |
var postRepo = new PostRepository(); | |
//Load all of our authentication handlers | |
require('./routes/auth')(everyauth, userRepo); | |
// Configuration | |
app.configure(function(){ | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.set('view options', { pretty: true }); | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser()); | |
app.use(express.methodOverride()); | |
app.use(express.session({secret:'5fdasd9034sfds'})); | |
app.use(everyauth.middleware()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
}); | |
// Routes | |
require('./routes/site')(app, postRepo); | |
require('./routes/posts')(app, postRepo); | |
//Configure everyauth view helpers | |
everyauth.helpExpress(app); | |
app.listen(process.env.PORT || 3000); | |
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment