Skip to content

Instantly share code, notes, and snippets.

@Arlen22
Forked from anonymous/index.js
Last active May 21, 2021 13:08
Show Gist options
  • Save Arlen22/bbd852f68e328165e49f to your computer and use it in GitHub Desktop.
Save Arlen22/bbd852f68e328165e49f to your computer and use it in GitHub Desktop.
A file to allow running multiple TiddlyWikis as seperate folders of an Express JS application. Made with 5.1.9. Amended for 5.1.14-Prerelease. My part is released into the Public Domain.
  • Download the NodeJS version of TiddlyWiki from GitHub or NPM. If you use NPM, do not use the global option (-g).
  • Put web.js beside tiddlywiki.js.
  • Require web.js from the Express application and call it on every matching request (see below).
  • For TiddlyWiki before 5.1.14-Prerelease: In core/modules/commands/server.js, seperate out the requestHandler function that is passed to http.createServer so it is on the SimpleServer prototype. Add the self variable to the first line of the requestHandler as shown.
SimpleServer.prototype.listen = function(port,host) {
	http.createServer(this.requestHandler.bind(this)).listen(port,host);
};
SimpleServer.prototype.requestHandler = function(request,response) {
	var self = this;
	// Compose the state object
	var state = {};
//In Express app
var tw5serve = require( path.join( 'path', 'to', 'web.js' ) );
//the first argument is the mount path, and the second is the data
//folder ./data/datafolder relative to web.js
app.all( '/tw5/*', tw5serve('tw5','tw5DataFolder');
//add more if you want:
app.all( '/mark/*', tw5serve('mark','markDataFolder');
app.all( '/john/*', tw5serve('john','johnDataFolder');
app.all( '/dave/*', tw5serve('dave','daveDataFolder');

Each mount needs its own data folder, but it uses the same code files.

Running on the plain Node HTTP server is not much different. tw5serve simply returns the request handler function which gets called with the request and response objects for each request.

const tw5serve = require( path.join( 'path', 'to', 'web.js' ) );
//the first argument is the mount path, and the second is the datafolder ./data/datafolder relative to web.js
const pref_tw5 = tw5serve('tw5','tw5DataFolder');
//add more if you want:
const pref_mark = tw5serve('mark','markDataFolder');

http.createServer((req, res)=>{
	if(/*req.url matches /tw5/*  */){
		pref_tw5(req, res);
	} else if (/*req.url matches /mark/*  */) {
		pref_mark(req, res);
	} else {
		res.writeHead(404);
		res.end();
	}
}).listen(8080, '0.0.0.0')

By modifying web.js, a separate username and password could be set for each directory. Or the same for all directories. The TiddlyWiki server uses basic authentication which transmits the password in plain text. Therefore it should only be used with either https or on a completely trusted network or on localhost only.

#!/usr/bin/env node
var url = require('url');
var path = require('path');
module.exports = function(prefix, dataFolder){
$tw = require("./boot/boot.js").TiddlyWiki();
$tw.boot.argv = [path.join( __dirname, 'data', dataFolder)];
$tw.boot.boot();
$tw.wiki.addTiddler({
"text": "$protocol$//$host$/" + prefix + "/",
"title": "$:/config/tiddlyweb/host"
});
//use $tw.modules.execute so the module has its correct $tw variable
//not sure why I didn't run into this before 5.1.14
var serverCommand = $tw.modules.execute('./core/modules/commands/server.js').Command;
var command = new serverCommand([], { wiki: $tw.wiki });
var server = command.server;
server.set({
rootTiddler: "$:/core/save/all",
renderType: "text/plain",
serveType: "text/html",
username: "",
password: "",
pathprefix: "/" + prefix
});
return server.requestHandler.bind(server);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment