Poet – a node.js blogging platform

Poet is a great little node.js based project that allows you to start a blog in no time.

TLDR; Put a bunch of MarkDown files in a folder and it’s available as a blog to the rest of the world. I just started a new project for JavaScript and I of course wanted to run it on JavaScript, even before GHOST was released, so I went with Poet.

The description of the project by the author made me smile, so I’ll just quickly share this quote:

“Cool blog, is this WordPress or something?” your square friend asks. “Nah dude, this is in node,” you respond, while skateboarding off into the sunset, doing mad flips and stuff.

So obviously this guy has a sense of humor, but what’s poet really about?

Inside a poet blog the file structure looks like this:

poet_file_structure

(The processes.js file is mine, I’m using pm2 to manage my node threads)

Poet does fine without a database and relies on a number of MarkDown formatted files, that have some extra info in them, to support a category, tags and a submission date.The meta-data usually look like this:

{{{
    "title" : "Wicked Cool Stuff",
    "tags"  : [ "Werewolf", "Vampire" ],
    "category" : "non-fiction",
    "date" : "07-19-2013"
}}}

To be honest, I find this date format ridiculous and I changed it to Year-Month-Date, as in MySQL dates. It’s not a problem to do this and requires minimal changes in the views. By default poet relies on jade as the templating engine, which is not my taste either, but it was fun actually writing some of it for a change. Underneath, it makes use of express.

If you’re looking for a super simple blog, that doesn’t require more from you than node.js installed and a bunch of MarkDown files, this is brilliant for you.

Generate Sitemap.xml with Poet

I started up a fresh news blog, that I expect to receive some search engine hits, so I thought a sitemap couldn’t hurt. Since there already was an RSS example in the github repository of poet, I could easily derive an example on how to set up a sitemap.xml. So this is what the file I run with node would look like:

var
  express = require('express'),
  app = express(),
  poet = require('poet')(app);

poet.init().then(function () {
  // initialized
});

app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(app.router);

app.get('/', function (req, res) { res.render('index'); });

app.get('/sitemap.xml', function (req, res) {
  // Only get the latest posts
  var postCount = poet.helpers.getPostCount();
  var posts = poet.helpers.getPosts(0, postCount);
  res.setHeader('Content-Type', 'application/xml');
  res.render('sitemap', { posts: posts });
});

app.listen(3000);

This makes the route sitemap.xml available in the system and now we just need the corresponding view file, which I called sitemap.jade.

doctype xml
urlset(xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″)
  each post in posts
    – var fullUrl = ‘http://localhost’ + post.url
      url
        loc= fullUrl

My example has actually been accepted into the poet repository now, you can see the pull request here. 😀

var
  express = require(‘express’),
  app = express(),
  poet = require(‘../lib/poet’)(app);
poet.init().then(function () {
  // initialized
});
app.set(‘view engine’, ‘jade’);
app.set(‘views’, __dirname + ‘/views’);
app.use(express.static(__dirname + ‘/public’));
app.use(app.router);
app.get(‘/’, function (req, res) { res.render(‘index’); });
app.get(‘/sitemap.xml’, function (req, res) {
  // Only get the latest posts
  var postCount = poet.helpers.getPostCount();
  var posts = poet.helpers.getPosts(0, postCount);
  res.setHeader(‘Content-Type’, ‘application/xml’);
  res.render(‘sitemap’, { posts: posts });
});
app.listen(3000);

Leave a Reply

Your email address will not be published. Required fields are marked *