From 327ab496220f3528853d10ee3d3141bc5a13f04c Mon Sep 17 00:00:00 2001 From: Nicholas O'Leary Date: Fri, 13 Sep 2013 23:22:57 +0100 Subject: [PATCH] Fixes #19 - httpRoot and httpAuth not taking effect We were attaching the editor app rather than the main app to the server, which meant the root and auth routes were ignored. --- red.js | 30 ++++++++++++++---------------- red/ui.js | 10 ++++++++-- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/red.js b/red.js index a8bb45f98..b3ea64e8a 100644 --- a/red.js +++ b/red.js @@ -17,34 +17,32 @@ var http = require('http'); var https = require('https'); var util = require("util"); var express = require("express"); +var crypto = require("crypto"); var settings = require("./settings"); -var redApp = null; - -if (settings.https) { - server = https.createServer(settings.https,function(req,res){redApp(req,res);}); -} else { - server = http.createServer(function(req,res){redApp(req,res);}); -} - -redApp = require('./red/server.js').init(server,settings); - var server; var app = express(); -settings.httpRoot = settings.httpRoot||""; -if (settings.httpRoot.slice(-1) == "/") { - settings.httpRoot = settings.httpRoot.slice(0,-1); +var redApp = null; + +if (settings.https) { + server = https.createServer(settings.https,function(req,res){app(req,res);}); +} else { + server = http.createServer(function(req,res){app(req,res);}); } + +redApp = require('./red/server.js').init(server,settings); + +settings.httpRoot = settings.httpRoot||"/"; + if (settings.httpRoot[0] != "/") { settings.httpRoot = "/"+settings.httpRoot; } -if (settings.httpRoot == "/") { - settings.httpRoot = ""; +if (settings.httpRoot.slice(-1) != "/") { + settings.httpRoot = settings.httpRoot + "/"; } settings.uiPort = settings.uiPort||1880; - if (settings.httpAuth) { app.use(settings.httpRoot, express.basicAuth(function(user, pass) { diff --git a/red/ui.js b/red/ui.js index 51f47ba04..809ca8e4a 100644 --- a/red/ui.js +++ b/red/ui.js @@ -21,8 +21,14 @@ var app = express(); function setupUI(settings) { - app.get(/^$/,function(req,res) { - res.redirect("/"); + // Need to ensure the url ends with a '/' so the static serving works + // with relative paths + app.get("/",function(req,res) { + if (req.originalUrl.slice(-1) != "/") { + res.redirect(req.originalUrl+"/"); + } else { + req.next(); + } }); app.use("/",express.static(__dirname + '/../public'));