Created
July 19, 2012 21:27
-
-
Save mikevalstar/3146946 to your computer and use it in GitHub Desktop.
Coding with Node.js Part 6
This file contains hidden or 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
| img_sm : { type: String }, | |
| content : { type: String }, | |
| short : { type: String }, | |
| group : { type: String }, /* add Group to DB */ | |
| ext_link: { type: String }, | |
| posted : { type: Date, index: true, default: Date.now }, | |
| edited : { type: Date, default: Date.now }, |
This file contains hidden or 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
| h3 Group | |
| input(type="text", name="group", style="width: 100%;", value=post.group) |
This file contains hidden or 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
| , pageIndex: function(req, res, nav){ | |
| var perpage = (!nav || nav.page) ? 10 : 100; // show 100 items for month or search, otherwise 10 | |
| var blogpost = this.db.model('blogPost'); | |
| var page = (nav && nav.page) ? nav.page - 1 : 0; | |
| var query = {}; | |
| if(nav && nav.search) query = {title: {$regex: new RegExp('.*' + Util.escapeRegEx(nav.search) + '.*', 'i')}}; | |
| if(nav && nav.group) query = {group: nav.group}; | |
| if(nav && nav.year){ | |
| var start = new Date(nav.year, nav.month, 1); | |
| var end = new Date(nav.year, start.getMonth() + 1, 1); | |
| query = {posted: { $gte: start, $lt: end }}; | |
| } | |
| var query = blogpost.find(query); | |
| query.sort("posted", -1).skip(page * perpage).limit(perpage).exec(function(err, docs){ | |
| for ( var k in docs ){ | |
| docs[k].link_title = Util.link_title(docs[k].title); | |
| var new_short = jade.compile(docs[k].short); | |
| docs[k].short = new_short(); | |
| docs[k].posted_human = Util.human_date(docs[k].posted); | |
| } | |
| query.count(function(err, postcount){ | |
| var totalpages = Math.ceil(postcount / perpage); | |
| var nextpage = totalpages > page ? page + 2 : false; | |
| var prevpage = page > 0 ? page : false; | |
| var render_page = function(monthlist){ | |
| res.render('index', { | |
| title: 'Blog', | |
| posts: docs, | |
| nextpage: nextpage, | |
| prevpage: prevpage, | |
| months: monthlist, | |
| month_names: new Array("January","February","March","April","May","June","July","August","September","October","November","December"), | |
| showFullNav: nav | |
| }); | |
| } | |
| // Production of month groups for Archive | |
| var map = function(){ | |
| emit( this.posted.getFullYear() + '-' + this.posted.getMonth() , { count: 1, year: this.posted.getFullYear(), month: this.posted.getMonth() } ); | |
| }; | |
| var reduce = function(key, values){ | |
| var result = { | |
| count: values.length, | |
| month: values[0].month, | |
| year: values[0].year | |
| }; | |
| return result; | |
| }; | |
| blogpost.collection.mapReduce( | |
| map.toString() | |
| , reduce.toString() | |
| , {out: { inline: 1 }} | |
| , function(err, monthlist){ | |
| monthlist.reverse(); | |
| render_page(monthlist); | |
| } | |
| ); | |
| }); | |
| }); | |
| } |
This file contains hidden or 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
| , initPages: function(app, db){ | |
| this.db = db; | |
| var self = this; | |
| app.get('/', function(req, res) { self.pageIndex(req, res, true); }); | |
| app.get('/Blog', function(req, res) { self.pageIndex(req, res, false); }); | |
| app.get('/Blog/:page([0-9]+)', function(req, res) { self.pageIndex(req, res, {page: req.params.page}); }); | |
| app.get('/Blog/:year([0-9]+)/:month([0-9]+)', function(req, res) { self.pageIndex(req, res, {year: req.params.year, month: req.params.month}); }); | |
| //app.get('/Blog/:group', function(req, res) { self.pageIndex(req, res, {group: req.params.group}); }); | |
| app.get('/Blog/s/:topic', function(req, res) { self.pageIndex(req, res, {search: req.params.topic}); }); | |
| app.get('/Blog/:id/:title', function(req, res) { self.pageBlogPost(req, res); }); | |
| app.get('/RSS', function(req, res) { self.RSS(req, res); }); | |
| } | |
This file contains hidden or 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
| escapeRegEx: function(str) { | |
| return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment