Skip to content

Instantly share code, notes, and snippets.

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:}).save();
list: function(req, res) {
Thread.find(function(err, 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);'/thread',;
app.get('/thread', api.list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment