Hello Poet!

7.19.2015

The first time I get a glimpse on Poet was about two years ago. I just started to teach Node.js in class and I was looking around for suitable projects. The idea to have a blog written completely by my self has never left me since then. Finally I had the last a few days time free to start my own written blogging app with Poet. There was great help from Jordan Santell maintaining Poet, and from his homepage http://jsantell.com. Also from Loïc Guillois and his poet powered blog Loïc Guillois | conception d'applications Web, Mobile et TV. Both sources together give me the starting point I needed.

You need a webspace with node support

After a brief search in the world wide web, I decided to use uberspace.de.

Uberspace.de ist eine Plattform von Technikern für Techniker und alle, die es werden wollen. Wir machen Hosting für Kommandozeilenliebhaber, Datenschützer, Kontrollebehalter, Unixfreunde, Selbermacher, Waszusagenhaber. Und wenn es mal klemmt, stehen dir erfahrene Linux-Admins zur Seite.

Ubersprace is young, sympathetic and you get a linux command line.

[wolffwo@dubhe poetBlog]$ uname -or
2.6.32-504.16.2.el6.x86_64 GNU/Linux
[wolffwo@dubhe ~]$ cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m

Setting up the first node server on Uberspace

There is already node installed, but I update to version v0.12.7 simply by

[wolffwo@dubhe ~]$ echo 'export PATH=/package/host/localhost/nodejs-0.12/bin:$PATH' >> ~/.bash_profile
[wolffwo@dubhe ~]$  source ~/.bash_profile
[wolffwo@dubhe ~]$  node -v
v0.12.7

Before starting to explain the details, let's have a look at the file structure of the project.

basicPoetDir
│  package.json
│  server.js
│  README.md
│
├───_posts
│   │ hello.md
│
├───public
│   │
│   └───css
│       │layout.css
│
└───views
    │  layout.jade
    │  index.jade
    │  ...  
    │
    └───includes/postSnippet
        │   postSnippet.jade
        │   pagination.jade

Create all directories and files before fill in the content!

Let's start with the package.json file. This file can be created in any directory, I will use as basic directory basicPoetDir in the home folder.

{
  "name" : "myPoetMiniHackBlog",
  "version" : "0.0.1",
  "main" : "server.js",
  "private" : true,
  "dependencies" : {
    "poet"    : ">= 0.1.6",
    "jade"    : ">= 0.27.5",
    "express" : "= 3.0.0rc5"
  }
}

and install all dependencies with the node package manager:

[wolffwo@dubhe ~]$ npm install

The File server.js realized the basic poet app:

var
  express = require('express'),
  app = express(),
  Poet = require('../lib/poet');

var poet = Poet(app, {
  postsPerPage: 3,
  posts: './_posts',
  metaFormat: 'json',
  routes: {
    '/myposts/:post': 'post',
    '/pagination/:page': 'page',
    '/mytags/:tag': 'tag',
    '/mycategories/:category': 'category'
  }
});

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

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

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

app.listen(66666);
console.log('Server running at http://127.0.0.1:66666/');

To get the app working you must create two directories: views and default

[wolffwo@dubhe ~]$ cd html
[wolffwo@dubhe html]$ mkdir views default

Inside the views directory you must store the template files layout.jade

doctype html
html
  head
    title= 'Poet Demo'
    meta(charset='utf-8')
    link(rel='stylesheet', href='/css/layout.css')
    link(rel='stylesheet', href='/css/style.css')  
  body
    #back.fixed
    #header.fixed
      .centered
        h1
          a.brand(href='/') Poet Demo
    #main
      block content
    #sidebar.fixed
      li.nav-header
        h4 categories
      - each cat in getCategories()
        li
          a(href=categoryURL(cat)) #{cat}
      li.nav-header
        h4 tags
      - each tag in getTags()
        - var numberTag = "tagCloundCategory_" + postsWithTag(tag).length
        span(class="#{numberTag}")
          a(href=tagURL(tag)) #{tag}
    #footer
      h1 footer

and index.jade

extends layout

block content
  - var posts = getPosts(0,5)
  - var page  = 1
  - each post in posts
    include includes/postSnippet

  include includes/pagination

What is still missing is the bridge from the apache web server to the node service. This is easiest to adjust with a .htaccess file. The .htaccess file must be created in DocumentRoot (the directory http in the home folder) of the apache web server.

RewriteEngine On
RewriteRule (.*) http://localhost:66666/$1   [P]

So now you can start your node powered blog app for the first time:

[wolffwo@dubhe basicPoetDir]$ node server.js
Server running at http://127.0.0.1:6666/

[wolffwo@dubhe ~]$ uberspace-setup-service poetBlog ~/poetBlog/node_modules/nodemon/bin/nodemon.js ~/poetBlog/ Creating the ~/etc/run-poetBlog/run service run script Creating the ~/etc/run-poetBlog/log/run logging run script Symlinking ~/etc/run-poetBlog to ~/service/poetBlog to start the service Waiting for the service to start ... 1 2 started!

Congratulations - the ~/service/poetBlog service is now ready to use! To control your service you'll need the svc command (hint: svc = service control):

To start the service (hint: u = up): svc -u ~/service/poetBlog To stop the service (hint: d = down): svc -d ~/service/poetBlog To reload the service (hint: h = HUP): svc -h ~/service/poetBlog To restart the service (hint: du = down, up): svc -du ~/service/poetBlog

To remove the service: cd ~/service/poetBlog rm ~/service/poetBlog svc -dx . log rm -rf ~/etc/run-poetBlog

More information about controlling daemons can be found here: https://uberspace.de/dokuwiki/system:daemontools#wenn_der_daemon_laeuft ```