From 9db2f18b5effe64495ba63cab6c912468cceb1ff Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Thu, 19 May 2011 17:36:26 +0100 Subject: [PATCH] Use Express to serve static Files --- node/server.js | 181 +++++++++-------------- node/settings.js | 1 + package.json | 3 +- settings.json | 4 +- static/{padhtml => pad.html} | 0 static/padhtml_old | 268 ----------------------------------- 6 files changed, 76 insertions(+), 381 deletions(-) rename static/{padhtml => pad.html} (100%) delete mode 100644 static/padhtml_old diff --git a/node/server.js b/node/server.js index 9a9a2a754..dd03c4359 100644 --- a/node/server.js +++ b/node/server.js @@ -14,67 +14,90 @@ * limitations under the License. */ -var http = require('http') - , url = require('url') - , fs = require('fs') - , socketio = require('socket.io') - , sys = require('sys') - , settings = require('./settings') - , db = require('./db') - , async = require('async'); - require('joose'); +//var http = require('http') +//var url = require('url') +var socketio = require('socket.io') +var settings = require('./settings') +var db = require('./db') +var async = require('async'); +var express = require('express'); +var path = require('path'); + +var serverName = "Etherpad-Lite ( http://j.mp/ep-lite )"; + async.waterfall([ + //initalize the database function (callback) { db.init(callback); }, + //initalize the http server function (callback) { - var server = http.createServer(function(req, res){ - var path = url.parse(req.url).pathname; - - if(path.substring(0,"/static".length) == "/static" || path.substring(0,"/p/".length) == "/p/") - { - if(path.substring(0,"/p/".length) == "/p/") - { - if(path.length < 7) - send404(res, path); - - path = "/static/padhtml"; - } - - sendFile(res, path, __dirname + "/.." + path); - } - else if(path == "/") - { - sendRedirect(res, path, "/p/test"); - } - else if(path == "/newpad") - { - sendRedirect(res, path, "/p/" + randomPadName()); - } - else if(path == "/ep/pad/reconnect") - { - if(req.headers.referer != null) - sendRedirect(res, path, req.headers.referer); - else - send404(res, path); - } - else - { - send404(res, path); - } + //create server + var app = express.createServer(); + + //set logging + if(settings.logHTTP) + app.use(express.logger({ format: ':date: :status, :method :url' })); + + //serve static files + app.get('/static/*', function(req, res) + { + res.header("Server", serverName); + var filePath = path.normalize(__dirname + "/.." + req.url); + res.sendfile(filePath, { maxAge: 1000*60*60 }); }); - - server.listen(settings.port); + + //serve pad.html under /p + app.get('/p/:pad', function(req, res) + { + res.header("Server", serverName); + var filePath = path.normalize(__dirname + "/../static/pad.html"); + res.sendfile(filePath, { maxAge: 1000*60*60 }); + }); + + //serve index.html under / + app.get('/', function(req, res) + { + res.header("Server", serverName); + var filePath = path.normalize(__dirname + "/../static/index.html"); + res.sendfile(filePath, { maxAge: 1000*60*60 }); + }); + + //serve robots.txt + app.get('/robots.txt', function(req, res) + { + res.header("Server", serverName); + var filePath = path.normalize(__dirname + "/../static/robots.txt"); + res.sendfile(filePath, { maxAge: 1000*60*60 }); + }); + + //serve favicon.ico + app.get('/favicon.ico', function(req, res) + { + res.header("Server", serverName); + var filePath = path.normalize(__dirname + "/../static/favicon.ico"); + res.sendfile(filePath, { maxAge: 1000*60*60 }); + }); + + //redirect the newpad requests + app.get('/newpad', function(req, res) + { + res.header("Server", serverName); + res.redirect('/p/' + randomPadName()); + }); + + //let the server listen + app.listen(settings.port); console.log("Server is listening at port " + settings.port); - var io = socketio.listen(server); + //init socket.io and redirect all requests to the MessageHandler + var io = socketio.listen(app); var messageHandler = require("./MessageHandler"); messageHandler.setSocketIO(io); - io.on('connection', function(client){ try{ messageHandler.handleConnect(client); @@ -108,67 +131,3 @@ function randomPadName() } return randomstring; } - -function sendFile(res, reqPath, path) -{ - fs.readFile(path, function(err, data){ - if (err){ - send404(res, reqPath); - } else { - var contentType = "text/html"; - - if (path.substring(path.length -3, path.length) == ".js") - contentType = "text/javascript"; - else if (path.substring(path.length -4, path.length) == ".css") - contentType = "text/css"; - else if (path.substring(path.length -4, path.length) == ".gif") - contentType = "image/gif"; - - res.writeHead(200, {'Content-Type': contentType}); - res.write(data, 'utf8'); - res.end(); - - requestLog(200, reqPath, "-> " + path); - } - }); -} - -function send404(res, reqPath) -{ - res.writeHead(404); - res.write("404 - Not Found"); - res.end(); - - requestLog(404, reqPath, "NOT FOUND!"); -} - -function sendRedirect(res, reqPath, location) -{ - res.writeHead(302, {'Location': location}); - res.end(); - - requestLog(302, reqPath, "-> " + location); -} - -function requestLog(code, path, desc) -{ - console.log(code +", " + path + ", " + desc); -} - -function errorlog(e) -{ - var timeStr = new Date().toUTCString() + ": "; - - if(typeof e == "string") - { - console.error(timeStr + e); - } - else if(e.stack != null) - { - console.error(timeStr + e.stack); - } - else - { - console.error(timeStr + JSON.stringify(e)); - } -} diff --git a/node/settings.js b/node/settings.js index 81abd3b0f..afa622c5a 100644 --- a/node/settings.js +++ b/node/settings.js @@ -20,6 +20,7 @@ var fs = require("fs"); exports.port = 9001; exports.dbType = "sqlite"; exports.dbSettings = { "filename" : "../var/sqlite.db" }; +exports.logHTTP = true; //read the settings sync var settingsStr = fs.readFileSync("../settings.json").toString(); diff --git a/package.json b/package.json index 5706a6a51..cc227961d 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "socket.io" : ">=0.6.17", "ueberDB" : ">=0.0.3", "async" : ">=0.1.9", - "joose" : ">=3.18.0" + "joose" : ">=3.18.0", + "express" : ">=2.3.4" }, "version" : "0.0.1", "bin" : { diff --git a/settings.json b/settings.json index c8b2b3274..cd79943e9 100644 --- a/settings.json +++ b/settings.json @@ -9,7 +9,7 @@ This file must be valid JSON. But comments are allowed //the database specific settings "dbSettings" : { "filename" : "../var/sqlite.db" - } + }, /* An Example of MySQL Configuration "dbType" : "mysql", @@ -20,4 +20,6 @@ This file must be valid JSON. But comments are allowed "database": "store" } */ + + "logHTTP" : true } diff --git a/static/padhtml b/static/pad.html similarity index 100% rename from static/padhtml rename to static/pad.html diff --git a/static/padhtml_old b/static/padhtml_old deleted file mode 100644 index 9228ef00d..000000000 --- a/static/padhtml_old +++ /dev/null @@ -1,268 +0,0 @@ - - - - - - -Etherpad Lite v0.0.1: test - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -    - - - - - - - - - - - -   -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - -    - - - - - - - - - - - - - - - - - - - - - -    - - - - - - -
- -
- - -
-
    -
-
- Save -
-
- Cancel -
-
-
-
-
-
-
- -
- -
- -
-
- Loading Etherpad Lite... -
-
- - - -
-
- -
-
-
-
-
-
- Connecting... -
-
- Reestablishing connection... -
-
-

Disconnected.

-

Opened in another window.

-

No Authorization.

-
-

- We're having trouble talking to the EtherPad lite synchronization server. - You may be connecting through an incompatible firewall or proxy server. -

-
-
-

- We were unable to connect to the EtherPad lite synchronization server. - This may be due to an incompatibility with your web browser or internet connection. -

-
-
-

- You seem to have opened this pad in another browser window. - If you'd like to use this window instead, you can reconnect. -

-
-
-

- Lost connection with the EtherPad lite synchronization server. This may be due to a loss of network connectivity. -

-
-
-

- Server not responding. This may be due to network connectivity issues or high load on the server. -

-
-
-

- Your browser's credentials or permissions have changed while viewing this pad. Try reconnecting. -

-
-
-

- If this continues to happen, please let us know - (opens in new window). -

-
-
- -
-
-
-
-
- - - - - \ No newline at end of file