Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.