Skip to content

Instantly share code, notes, and snippets.

@criso
Forked from fwielstra/api.js
Created July 24, 2011 15:44
Show Gist options
  • Save criso/1102742 to your computer and use it in GitHub Desktop.
Save criso/1102742 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