TW5 served by Express

Thursday, 24 Apr. 2014

In the second step we are going to build a single custom TW5 on the RPI and use the output folder as root for the "static" express server.

create a new edition in /opt/tw/TiddlyWiki5/editions :

cd /opt/tw/TiddlyWiki5/editions
mkdir rpi
cd rpi
mkdir tiddlers

create a tiddlywiki.info

nano tiddlywiki.info

paste:

{
        "plugins": [
                "tiddlywiki/fullscreen",
                "tiddlywiki/d3"
        ],
        "themes": [
                "tiddlywiki/snowwhite"
        ]
}

this also includes the d3 plugin to prepare this tiddlywiki edition for data (remove the line "tiddlywiki/d3" if you don't need it)

next ...

create a target folder for the build process (before the TiddlyWiki5 folder)

cd /opt/tw
mkdir rpitw

go back to TiddlyWiki5 folder and create a new build script

nano rpibuild.sh

paste:

#!/bin/bash
 
# build TiddlyWiki5 for five.tiddlywiki.com
 
# Set up the build output directory
 
if [  -z "$TW5_BUILD_OUTPUT" ]; then
#    TW5_BUILD_OUTPUT=../jermolene.github.com
    TW5_BUILD_OUTPUT=../rpitw
fi
 
if [  ! -d "$TW5_BUILD_OUTPUT" ]; then
    echo 'A valid TW5_BUILD_OUTPUT environment variable must be set'
    exit 1
fi
 
echo "Using TW5_BUILD_OUTPUT as [$TW5_BUILD_OUTPUT]"
 
# Build a TiddlyWiki containing the content editions/rpi/tiddlers
 
node ./tiddlywiki.js \
        ./editions/rpi \
        --verbose \
        --rendertiddler $:/core/templates/tiddlywiki5.template.html $TW5_BUILD_OUTPUT/index.html text/plain \
        || exit 1

make rpibuild.sh executable

sudo chmod 755 /opt/tw/TiddlyWiki5/rpibuild.sh

test run:

./rpibuild.sh

cd to /opt/tw/rpitw and check if index.html has been build

next install express

cd /opt/tw/TiddlyWiki5
mkdir server
cd server
mkdir express
cd express

verify current express version

npm info express version

...and use that in the package.json ... next

nano package.json

paste:

{
  "name": "tw5server",
  "description": "tw5 express server",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express": "3.3.8"
  }
}

install express

npm install

check:

npm ls

create a test:

nano app.js

paste:

var express = require('express');
var app = express();
 
app.get('/', function(req, res){
  res.send('hello world');
});
 
app.listen(3000);
console.log('Listening on port 3000');

test the app:

node app

Ctrl-c to stop de server

Next

create a static express server that uses the TiddlyWiki build output as the server root

create a new app

cd server/express
nano tw.js

paste:

var express = require('express');
var app = express();
var path = require('path');
 
app.use(express.static(path.join(__dirname, '../../../rpitw/'))); //  folder "rpitw" is root serving an index.html
 
app.listen(3000);
console.log('Listening on port 3000');

start the server

node tw.js

check in a browser with your local ip

(example)

192.168.1.68:3000

back in the terminal Ctrl-c to stop the server again