Skip to content
Browse files

updead bogart-couchdb article to reflect changes to bogart

  • Loading branch information...
1 parent 3aaad0d commit a1a9b23216e3268bf7fff5ad1812b4c2b810cf8f @nrstott nrstott committed Dec 14, 2011
Showing with 63 additions and 49 deletions.
  1. +56 −45 articles/bogart-couchdb/app.js
  2. +2 −1 articles/bogart-couchdb/package.json
  3. +5 −3 articles/bogart-couchdb/syncDesignDoc.js
View
101 articles/bogart-couchdb/app.js
@@ -1,66 +1,77 @@
-var bogart = require('bogart');
-var couchdb = require('couchdb');
+var bogart = require('bogart')
+ , couchdb = require('couchdb')
+ , settings = require('./settings');
-var app = bogart.router(function(get, post, put, destroy) {
+var client = couchdb.createClient(settings.port, settings.host, { user: settings.user, password: settings.password })
+ , db = client.db(settings.db)
+ , viewEngine = bogart.viewEngine('mustache')
+ , router = bogart.router();
- var client = couchdb.createClient(5984, '127.0.0.1', { user: 'nathan', password: 's4stott' })
- , db = client.db('blog')
- , viewEngine = bogart.viewEngine('mustache');
+router.get('/', function(req) {
+ return bogart.redirect('/posts');
+});
- get('/posts', function(req) {
+router.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', {
+ 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: {
- title: 'New Post'
+ posts: posts,
+ title: 'Blog Home'
}
});
+ }, function(err) {
+ if (err.error && err.error === 'not_found') {
+ return bogart.error('execute syncDesignDoc before trying to use the blog');
+ }
+ throw err;
});
+});
+
+router.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';
+router.post('/posts', function(req) {
+ var post = req.params;
+ post.type = 'post';
- return db.saveDoc(post).then(function(resp) {
- return bogart.redirect('/posts');
- });
+ 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 });
- });
+router.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;
+router.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.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);
- });
+ 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);
+var app = bogart.app();
+
+// Include batteries, a default JSGI stack.
+app.use(bogart.batteries);
+
+// Include our router, it is significant that this is included after batteries.
+app.use(router);
-// Start the JSGI server.
-bogart.start(app);
+app.start();
View
3 articles/bogart-couchdb/package.json
@@ -9,6 +9,7 @@
"dependencies": {
"bogart": ">=0.2.0",
"mustache": "0.3.1-dev",
- "couchdb": ">=0.1.2"
+ "couchdb": ">=0.1.2",
+ "promised-io": "=0.2.3"
}
}
View
8 articles/bogart-couchdb/syncDesignDoc.js
@@ -1,6 +1,8 @@
-var couchdb = require('couchdb');
-var client = couchdb.createClient(5984, '127.0.0.1', { user: 'nathan', password: 's4stott' });
-var db = client.db('blog');
+var couchdb = require('couchdb')
+ , settings = require('../settings');
+
+var client = couchdb.createClient(settings.port, settings.host, { user: settings.user, password: settings.password });
+var db = client.db(settings.db);
var designDoc = {
_id: '_design/blog',

0 comments on commit a1a9b23

Please sign in to comment.
Something went wrong with that request. Please try again.