Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
307 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var bogart = require('bogart'); | ||
var couchdb = require('couchdb'); | ||
|
||
var app = bogart.router(function(get, post, put, destroy) { | ||
|
||
var client = couchdb.createClient(5984, '127.0.0.1', { user: 'nathan', password: 's4stott' }) | ||
, db = client.db('blog') | ||
, viewEngine = bogart.viewEngine('mustache'); | ||
|
||
get('/posts', function(req) { | ||
|
||
return db.view('blog', 'posts_by_date').then(function(resp) { | ||
var posts = resp.rows.map(function(x) { return x.value; }); | ||
|
||
return viewEngine.respond('posts.html', { | ||
locals: { | ||
posts: posts, | ||
title: 'Blog Home' | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
get('/posts/new', function(req) { | ||
return viewEngine.respond('new-post.html', { | ||
locals: { | ||
title: 'New Post' | ||
} | ||
}); | ||
}); | ||
|
||
post('/posts', function(req) { | ||
var post = req.params; | ||
post.type = 'post'; | ||
|
||
return db.saveDoc(post).then(function(resp) { | ||
return bogart.redirect('/posts'); | ||
}); | ||
}); | ||
|
||
get('/posts/:id', function(req) { | ||
return db.openDoc(req.params.id).then(function(post) { | ||
return viewEngine.respond('post.html', { locals: post }); | ||
}); | ||
}); | ||
|
||
post('/posts/:id/comments', function(req) { | ||
var comment = req.params; | ||
|
||
return db.openDoc(req.params.id).then(function(post) { | ||
post.comments = post.comments || []; | ||
post.comments.push(comment); | ||
|
||
return db.saveDoc(post).then(function(resp) { | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<a href='/posts'>Home</a> | ||
|
||
<h1>{{title}}</h1> | ||
|
||
<div class='post-content'> | ||
{{body}} | ||
</div> | ||
|
||
<div> | ||
<h2>Comments</h2> | ||
<ul id='comments'> | ||
{{#comments}} | ||
<li> | ||
<div> | ||
Author: <span class='author'>{{author}}</span> | ||
</div> | ||
<div> | ||
{{body}} | ||
</div> | ||
</li> | ||
{{/comments}} | ||
|
||
{{^comments}} | ||
No Comments Yet | ||
{{/comments}} | ||
</ul> | ||
</div> | ||
|
||
<form method='post' action='/posts/{{_id}}/comments'> | ||
<fieldset> | ||
<legend>Leave a Comment</legend> | ||
|
||
<div> | ||
<label for='author'>Your Name</label> | ||
<input name='author' /> | ||
</div> | ||
|
||
<div> | ||
<label for='body'>Your Comment</label> | ||
<input name='body' /> | ||
</div> | ||
|
||
<div class='buttons'> | ||
<input type='submit' value='Post Comment' /> | ||
</div> | ||
</fieldset> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<h2>Posts</h2> | ||
|
||
<a href='/posts/new'>New Post</a> | ||
|
||
<ul> | ||
{{#posts}} | ||
<li> | ||
<div class='post'> | ||
<h2><a href='/posts/{{_id}}'>{{title}}</a></h2> | ||
<p> | ||
{{body}} | ||
</p> | ||
</div> | ||
</li> | ||
{{/posts}} | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var couchdb = require('couchdb'); | ||
var client = couchdb.createClient(5984, '127.0.0.1', { user: 'nathan', password: 's4stott' }); | ||
var db = client.db('blog'); | ||
|
||
var designDoc = { | ||
_id: '_design/blog', | ||
|
||
language: 'javascript', | ||
|
||
views: { | ||
'posts_by_date': { | ||
map: function(doc) { | ||
if (doc.type === 'post') { | ||
emit(doc.postedAt, doc); | ||
} | ||
}.toString() | ||
} | ||
} | ||
}; | ||
|
||
db.saveDoc(designDoc).then(function(resp) { | ||
console.log('updated design doc!'); | ||
}, function(err) { | ||
console.log('error updating design doc: '+require('util').inspect(err)); | ||
}); |