public
Last active

Bogart CouchDB Blog code translated to use MongoDB

  • Download Gist
app.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
var bogart = require('bogart');
var Q = require('promised-io/lib/promise');
var mongoose = require('mongoose');
 
var PostSchema = new mongoose.Schema({
title: String,
body: String,
comments: [ CommentSchema ]
});
 
var CommentSchema = new mongoose.Schema({
author: String,
body: String
});
 
var Post = mongoose.model('PostSchema', PostSchema);
 
var app = bogart.router(function(get, post, put, destroy) {
 
var client = mongoose.connect('mongodb://localhost/test')
, viewEngine = bogart.viewEngine('mustache');
 
get('/posts', function(req) {
return Q.execute(Post.find.bind(Post)).then(function(docs) {
return viewEngine.respond('posts.html', {
locals: {
posts: docs,
title: 'Blog Home'
}
})
});
});
 
get('/posts/new', function(req) {
return viewEngine.respond('new-post.html', {
locals: {
title: 'New Post'
}
});
});
post('/posts', function(req) {
var post = new Post(req.params);
return Q.execute(post.save.bind(post)).then(function() {
return bogart.redirect('/posts');
});
});
 
get('/posts/:id', function(req) {
console.log('getting post');
 
return Q.execute(Post.findById.bind(Post), req.params.id).then(function(post) {
return viewEngine.respond('post.html', { locals: post });
});
});
 
post('/posts/:id/comments', function(req) {
var comment = req.params;
 
return Q.execute(Post.findById.bind(Post), req.params.id).then(function(post) {
post.comments = post.comments || [];
post.comments.push(comment);
 
return Q.execute(post.save.bind(post)).then(function() {
return bogart.redirect('/posts/'+req.params.id);
});
});
});
});
 
// Add ParseForm middleware to automatically process the parameters
// of form submissions into a JSON object that is easily usable (req.body, req.params).
app = bogart.middleware.ParseForm(app);
 
// Start the JSGI server.
bogart.start(app);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.