Sep 9, 2012

Free blog with Node.js and AWS

I made this weblog in one day. I did not use any ready made blogging platform. Instead I wrote the whole app in JavaScript+Node.js and connected it with a DynamoDB back-end. In this post I will walk through the process.

What's included

I made this blog completely from scratch because I wanted it to be simple and easy to develop further when needed. As you can see there are not that many sections. Only frontpage, about page, and a page for each posts. In addition I made an admin interface where I can create new posts and edit the existing ones.

Amazon services

Not everybody seem to know that you can run stuff on Amazon's platform for free for one year. The free-tier includes lots of stuff, and if you go over the limits you will be charged. So unless this blog becomes really big, I will be able to run it for free for the next year. To be able to access the free tier, you will need a credit card and a telephone (they always want to identify users with an automated phone call).

The whole site is now running on a EC2 micro-instance. All these posts are stored in a DynamoDB table.

Domain, SVN and other services

I got the domain from Namecheap. It cost less than 10 euros. Then I got an SVN account from Assembla. If you're not into SVN, they also have Git as a service. SVN service is free as well.

I was not sure about how to handle post comments. Making a custom comment thingy would be easy, but authentication is problematic. Facebook comment plugin is fairly nice, but it blocks out all other than Facebook users. So I decided to go with Disqus. They have a comment widget that supports multiple sign-in methods. It's free too :)

To be honest, I am not at all sure about Disqus. It was easy to embed to the pages, but I haven't had a chance to really test it. If anyone knows any better, I am listening.

I also added Google Analytics to be able to track the traffic to this site.

The Node.js app

I used the latest 0.8.8 Node.js distro with Express.js. The HTML templates are using EJS. While I like the Twitter Bootstrap, I decided to use Foundation as responsive front-end framework. This blog would have been fairly easy to do without any framework, but I did not want to redo all the responsive stuff myself.

The Node.js app works so that it loads all posts in the memory when it's started. That way I will keep the database traffic in minimum. Only when I post a new article or update an article, the posts will be reloaded from the database. I do the Express routing so that static routes will always override dynamic ones. So that I won't be able to override for example with a blog post with the same URL.

I made the post authoring to support Markdown. This makes the authoring easier and also makes it easier to alter the HTML layer later on. For the actual parsing I used Node-markdown module.

The DynamoDB table contains two keys: "url" is the hashkey as String, and "created" as a Number. In DynamoDB you only need to specify the keys (one key is enough) and then you can define any other fields for all items you store in a table. In addition to the keys, I am storing, title, tags, short text and the body text.

Other things to note

One thing to notice, is that I had to make the server to listen to port 8080 so that it can be started as a normal user. Then use iptables prerouting to route the port 80 requests to port 8080.

comments powered by Disqus