/*\ title: $:/core/modules/server.js type: application/javascript module-type: library Serve tiddlers over http \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; if($tw.node) { var util = require("util"), fs = require("fs"), url = require("url"), path = require("path"), http = require("http"); } /* A simple HTTP server with regexp-based routes options: variables - optional hashmap of variables to set (a misnomer - they are really constant parameters) routes - optional array of routes to use wiki - reference to wiki object */ function Server(options) { var self = this; this.routes = options.routes || []; this.wiki = options.wiki; this.variables = $tw.utils.extend({},this.defaultVariables,options.variables); // Add route handlers $tw.modules.forEachModuleOfType("serverroute", function(title,routeDefinition) { // console.log("Loading server route " + title); self.addRoute(routeDefinition); }); } Server.prototype.defaultVariables = { port: "8080", host: "127.0.0.1", rootTiddler: "$:/core/save/all", renderType: "text/plain", serveType: "text/html", debugLevel: "none" }; Server.prototype.set = function(obj) { var self = this; $tw.utils.each(obj,function(value,name) { self.variables[name] = value; }); }; Server.prototype.get = function(name) { return this.variables[name]; }; Server.prototype.addRoute = function(route) { this.routes.push(route); }; Server.prototype.findMatchingRoute = function(request,state) { var pathprefix = this.get("pathprefix") || ""; for(var t=0; t