Serving Assets

I'm now able to serve a basic html page; this doesn't mean much since I'm not going to have any in my final system, but this does let me continue to build the features I'm going to need. The first real feature should be the ability to serve the assets directory; this is where I will store my CSS, JavaScript, and image files.

I decided to use koa-static-cache, but configuring it correctly was a bigger challenge than I anticipated. I initially added it like so:

var statics = require('koa-static-cache');

I added the link tag in my html to a css file, however, it was not working as I expected. I tried several things to get it to work, like using the full path to the directory instead of a relative one. I even swapped out the whole module for a different one, to no avail. Eventually I found this Stack Overflow post and I saw what was going on. The statics module was working correctly, it's just the url to access the files doesn't include the assets subfolder. The css file could be accessed at '', but I wanted it to be ''. As an answer on Stack Overflow suggested, I used koa-mount to acheive this.

So the relevant code that is now correctly serving up my assets folder:

var statics = require('koa-static-cache');
var mount = require('koa-mount');
// Mount the assets directory
app.use(mount('/assets', statics('assets')));

Serving HTML

Building upon my 'Hello World' script, I wanted to simply return an html file for the landing page. Turns out that it's very easy to do.

var fs = require('co-fs');

router.get('/', function* (next){
  this.body = yield fs.readFile('home.html', 'utf8');
  this.response.status = 200;

I added 'co-fs' to access the file system functions like reading and writing files or folders, then updated my only route to read a home.html file and set the contents as the response body.

Writing a New Blog Engine

I’m never short on personal projects. I tend to get excited about something new I want to learn or work on and jump in right away. Inevitably something in life comes along which I make a higher priority (like having a baby), so when I finally make time to work on something I may or may not still be excited about that particular thing. This may be a character flaw that I need to work on but I’ll save that for another day.

Today I’m jumping in on a new project, to write a new blog engine. My desire for this is really to learn NodeJS; I’ve talked before about how I use my blog as a platform to learn new techniques and technologies. Not only does this blog need a facelift but there are some issues I have with the current framework the blog is written in. So I’m writing my own. It’ll be inspired by Casey LissCamel Project, but I’ll differentiate it in ways to best suit my workflow.

My desire is that as I work on the project I’ll write about some of the challenges I’m facing, as well as the reasoning behind some of the design decisions I’m making. I’ll post code snippets as well whenever I think it’s appropriate.

My first challenge was simply figuring out how to work within the Node ecosystem. Npm is the package manager for Node and you use it on the command line in order to add dependencies to a project. I’m used to the java world where you download the dependency jar and add it to the classpath or specifying it in a pom file. It’s a bit of a shift but I think I understand it now. Here’s the command I used to add the first dependency.

npm install —-save koa

NodeJS has a ton of libraries and thus there are many different ways to accomplish the same thing. When trying to pick the web app framework to use it came down to two, Express or Koa. Express is perhaps the most mature and widely used framework. Koa on the other hand is newish but a bit more forward looking; it embraces new javascript features that make writing a web app more enjoyable. Astute readers will notice from the command above that I choose Koa.

Below is my version of the classic ‘Hello world’ app.

var app = require('koa')();
var router = require('koa-router’)();

router.get('/', function\* (next){
  this.body = 'Hello World!';


console.log('Server listening on port 3000');