db.allDocs({include_docs: true,attachments: true},function(err,result){// If there was an error, log itif(err){console.log(err);}// Else render the 'posts' view and pass it all the postselse{res.status(200).json(result.rows);}});
DB Call to Create a new post
db.post(post,function(err,created){// If there was an error, log itif(err||!created){console.log(err);res.sendStatus(500);}else{returnres.status(201).json(created);}});
DB Call to GET a single post
db.get(req.params.id,function(err,found){// If there was an error, log itif(err||!found){console.log('Something went wrong');res.sendStatus(500);}// Else the post was foundelse{res.status(200).json(found);}});
DB Call to UPDATE a single post
db.get(req.params.id,function(err,found){// If there was an error, log itif(err||!found){console.log('Something went wrong');res.sendStatus(500);}// Else the post was foundelse{// Now save the updated postdb.put({_id: found._id,_rev: found._rev,title: req.body.title,post: req.body.post},function(err,saved){// If there was an error, log itif(err||!saved){console.log('Something went wrong');res.sendStatus(500);}else{res.status(201).json(saved);}});}});
DB Call to DELETE a single post
db.get(req.params.id,function(err,found){// If there was an error, log itif(err){console.log(err);// Redirect to the '/' routeres.sendStatus(500);}// Else the post was foundelse{// Remove the post from the databasedb.remove(found,function(err,removed){// If there was an error, log itif(err){console.log(err);res.sendStatus(500);}else{res.status(202).json(removed);}});}});
Frontend stuff
How to make DOM manipulations (change html) with JQuery