Over a million developers have joined DZone.

Update to my Node Blog Engine

DZone's Guide to

Update to my Node Blog Engine

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Just a quick Sunday post (mainly so I can avoid stressing over my Saints). A week or so I posted about my first attempt to build a "real" Node.js app with Express. My goal was to build a simple front end to my blog database. This gave me a simple enough application to learn Node/Express as well as plenty of data. I've been able to update the application quite a bit and I thought I'd share what I've done. As before, please do not consider this best pratice Node.js/Express development. I'm still very early in my Node education.

One of the first changes I've been able to make is with my view engine. I mentioned in the previous blog post how I wasn't a fan of the syntax that EJS used. As a reminder, here is what it looks like:


    entries.forEach(function(entry) {
        var d = new Date(entry.posted);
        var year = d.getFullYear();
        var month = d.getMonth()+1;
        var day = d.getDate();
    <li><a href="/<%= year + '/' + month + '/' + day %>/<%= entry.alias %>"><%= entry.title %></a> (<%= entry.posted %>)</li>
<% }) %>

I was able to switch my view engine to one that made use of Handlebars. The node package hbs allows you to use you Handlebars in your HTML templates. So the above becomes something a bit nicer:


{{#each entries}}
    <li><a href="{{bloglink this.posted this.alias}}">{{this.title}}</a> ({{this.posted}})</li>

To be fair to EJS, I also added a helper function "bloglink" to simplify the creation of the SES-style link. To make my application even more stylish (heh), I added a layout.html to my views page. This automatically wraps my views:

<!doctype html>
    <link rel="stylesheet" href="/static/styles.css" />


<header>Ray's Blog</header>


<a href="/">Home</a>


Obviously that isn't a full template, but it gives you a basic idea. Two things I want to call out here. Three things actually.

First, note the use of {{{body}}}. This is where the previously rendered view will be inserted.

Second, note the use of {{title}} on top. As long as I set a title value when rendering my view, this will be picked up and used, so for example, check out the code that renders a blog entry:

app.get("/:year/:month/:day/:alias", function(req, res) {
    var entryDate = new Date(req.params.year,req.params.month-1,req.params.day);
    blog.getBlogEntry(entryDate,req.params.alias,function(entry,comments) {
        res.render("entry", {entry:entry,comments:comments,title:entry.title});
    },function() {
        res.writeHead(302, {
            'Location': '/'

See the third argument passed to render? That's where I supported a dynamic title for a blog entry.

Finally - note the CSS link. Don't forget Express makes it easy to set up a folder of static stuff your application can use. Things like CSS files and JavaScript libraries. Here's how I did it:

app.use('/static', express.static(__dirname + "/public",{maxAge:86400000}));

So to me - being able to use Handlebars is huge. I'm a lot more comfortable with it than EJS and I found myself being a lot more productive as I built out the application. Excitement is a good thing, I think.

I made a few more changes that I want to share. As I mentioned in my previous post, app.js is the core application file for your Node.js file. It certainly isn't your only file, but it's the critical top level commander of everything your site does. With that being said, I have this feeling that I should keep things as clean as possible.

As an example, I built a utility library that would let me define Handlebars helpers (think formatting functions). This kept my app.js a bit cleaner. Here is the current version of it with a grand total of one helper now. I'll add more later.

getHelpers = function() {

var helpers = [];

helpers.push({name:"bloglink", helper:function(date, alias) {
var d = new Date(date);
var year = d.getFullYear();
var month = d.getMonth()+1;
var day = d.getDate();
return '/'+ year + '/' + month + '/' + day +'/'+ alias;

return helpers;

exports.addHelpers = function(hb) {
var helpers = getHelpers();
for(var i=0, len=helpers.length; i<len; i++) {
hb.registerHelper(helpers[i].name, helpers[i].helper);
return hb;

My app.js then can be a bit simpler:

var hbs = require('hbs');
//loads in a library of helpers and adds them to hbs
var helpers = require('./helpers.js');
hbs = helpers.addHelpers(hbs);

Finally, I also removed the custom RSS code from app.js. I moved it into my blog library instead. This is how it looks in app.js (and it's a good 1/5th the size):

app.get("/blog.rss", function(req, res) {

    blog.getRSS(function(xml) {
        res.writeHead(200, {'Content-Type':'application/rss+xml'});


For folks who want to see the complete code base, I've attached it below. Obviously you need data, and if anyone wants some sample data from my blog, just ask.

Download attached file

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.


Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}