Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up blog.js and moved some more stuff to blog-support/index.js…

…... blog.js is now 60-something lines
  • Loading branch information...
commit 84a2bc713caf9d9b90571af84beda4161b7830b0 1 parent 062d411
@jasonwyatt jasonwyatt authored committed
Showing with 41 additions and 51 deletions.
  1. +28 −1 examples/blog-support/index.js
  2. +13 −50 examples/blog.js
View
29 examples/blog-support/index.js
@@ -1,3 +1,6 @@
+// posts:
+// Array of posts to seed the database with. (taken from http://jwf.us,
+// by Jason Feinstein with permission)
exports.posts = [
{
_id: '1',
@@ -22,8 +25,33 @@ exports.posts = [
}
];
+// designDoc:
+// The CouchDB view page that will be used to get a reverse
+// chronologically-ordered list of posts for rendering.
+exports.designDoc = {
+ _id: "_design/blog",
+ language: "javascript",
+ views: {
+ posts_by_date: {
+ map: function(doc){
+ if(doc.type == "post"){
+ emit(-Date.parse(doc.date), doc);
+ }
+ }
+ }
+ }
+};
+
function CouchBlogRenderer(title){
+ // summary:
+ // Tool used to render the blog to HTML and RSS.
+ // description:
+ // Assumes blog posts will contain a format similar to those above
+ // and can render them to an RSS Feed, a main page (listing posts
+ // in reverse-chronological order), and individual pages.
+ // title: String
+ // Title of the blog, defaults to "Blog".
this.title = title || "Blog";
}
CouchBlogRenderer.prototype = {
@@ -92,5 +120,4 @@ CouchBlogRenderer.prototype = {
res.end();
}
};
-
exports.CouchBlogRenderer = CouchBlogRenderer;
View
63 examples/blog.js
@@ -9,67 +9,35 @@ var CouchDB = require('../lib').CouchDB;
var blogsupport = require('./blog-support');
var blogTitle = "node-couch example blog";
-var dbName = "node_couch_blog";
-var blogPort = 8080;
-var designDoc = {
- _id: "_design/blog",
- language: "javascript",
- views: {
- posts_by_date: {
- map: function(doc){
- if(doc.type == "post"){
- emit(-Date.parse(doc.date), doc);
- }
- }
- }
- }
-};
-
-var renderer = new blogsupport.CouchBlogRenderer(blogTitle);
+var dbName = "node_couch_blog";
+var blogPort = 8080;
+var renderer = new blogsupport.CouchBlogRenderer(blogTitle);
+var designDoc = blogsupport.designDoc;
+var postRegex = /^\/posts\/([a-f0-9]*)$/;
+var feedRegex = /^\/feed$/;
var db = CouchDB.db(dbName);
db.create({
success: function(res){
db.saveDoc(designDoc, {
- success: startBlog,
- error: function(error){
- sys.puts("error occurred when creating design doc: "+JSON.stringify(error));
- }
+ success: startBlog
});
- },
- error: function(error){
- sys.puts("Could not create database: "+JSON.stringify(error));
}
});
function startBlog(){
- // let's go ahead and fill up the blog with some posts...
for(var i = 0, len = blogsupport.posts.length; i < len; i++){
db.saveDoc(blogsupport.posts[i]);
}
-
- var postRegex = /^\/posts\/([a-f0-9]*)$/;
- var feedRegex = /^\/feed$/;
var server = http.createServer(function (req, res) {
- var method = req.method;
var pathDetails = url.parse(req.url, true);
- var headers = req.headers;
-
- var errFunc = function(error){
- res.writeHead(500, {'Content-Type': 'text/plain'});
- res.write("Error: "+JSON.stringify(error));
- res.end();
- }
if(pathDetails.pathname === "/"){
- sys.puts("Requested the main blog page.");
-
db.view("blog/posts_by_date", {
success: function(result){
renderer.renderMain(req, res, result.rows);
- },
- error: errFunc
+ }
});
} else if(postRegex.test(pathDetails.pathname)){
var match = postRegex.exec(pathDetails.pathname);
@@ -80,24 +48,19 @@ function startBlog(){
db.openDoc(docId, {
success: function(doc){
renderer.renderPostPage(req, res, doc);
- },
- error: errFunc
+ }
});
-
- sys.puts("Requested a post: "+decodeURIComponent(match[1]));
} else if(feedRegex.test(pathDetails.pathname)){
- sys.puts("Requested the feed");
db.view("blog/posts_by_date", {
success: function(result){
renderer.renderFeed(req, res, result.rows);
- },
- error: errFunc
+ }
});
} else {
- errFunc({});
+ res.writeHead(404, {'Content-Type': 'text/plain'});
+ res.end("Cannot find specified page: "+req.url);
}
}).listen(blogPort);
-
-
+
sys.puts("Blog started.");
}
Please sign in to comment.
Something went wrong with that request. Please try again.