Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fwielstra
Created June 14, 2011 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fwielstra/1025070 to your computer and use it in GitHub Desktop.
Save fwielstra/1025070 to your computer and use it in GitHub Desktop.
Our controller refactored
module.exports = function(Thread, Post) {
return {
post: function(req, res) {
new Thread({title: req.body.title, author: req.body.author}).save();
},
list: function(req, res) {
Thread.find(function(err, threads) {
res.send(threads);
});
},
show: function(req, res) {
Thread.findOne({title: req.params.title}, function(error, thread) {
var posts = Post.find({thread: thread._id}, function(error, posts) {
res.send([{thread: thread, posts: posts}]);
});
});
}
};
};
// Partial rewrite of app.js. You can see we moved our Thread and Post model initialization code to our app.js.
var Thread = require('./models/thread.js');
var Post = require('./models/post.js');
var api = require('./controllers/api.js')(Thread, Post);
app.post('/thread', api.post);
app.get('/thread/:title.:format?', api.show);
app.get('/thread', api.list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment