From f4b14df19590001433669052d33db508a75e3bc1 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sun, 28 Apr 2013 13:58:19 -0400 Subject: [PATCH] some more fixes to the session handling to ignore the /api folder, and refactoring how the public directory is set up in webserver.js. THIS COMMIT ALTERS NODEBB'S DEPENDENCIES! Please run npm install after pulling. --- app.js | 2 +- package.json | 3 ++- public/templates/login.tpl | 1 - src/webserver.js | 25 +++++++++---------------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index 8ab74cf957..8732c4aee4 100644 --- a/app.js +++ b/app.js @@ -22,7 +22,7 @@ global.modules = modules; config['ROOT_DIRECTORY'] = __dirname; modules.templates.init(); - modules.webserver.init(); + // modules.webserver.init(); modules.websockets.init(); diff --git a/package.json b/package.json index f51de29b2c..62729ea37c 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "connect": "2.7.6", "emailjs": "0.3.4", "cookie": "0.0.6", - "connect-redis": "1.4.5" + "connect-redis": "1.4.5", + "path": "0.4.9" }, "devDependencies": {}, "optionalDependencies": {}, diff --git a/public/templates/login.tpl b/public/templates/login.tpl index d4756927a7..e9a96e7862 100644 --- a/public/templates/login.tpl +++ b/public/templates/login.tpl @@ -25,7 +25,6 @@ ajaxify.register_events(['user.login']); socket.on('user.login', function(data) { - console.log(data); if (data.status === 0) { jQuery('#error').show(50); jQuery('#error p').html(data.message); diff --git a/src/webserver.js b/src/webserver.js index 4e32d25aa3..db5e72b2f7 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -1,7 +1,8 @@ var express = require('express'), WebServer = express(), server = require('http').createServer(WebServer), - RedisStore = require('connect-redis')(express); + RedisStore = require('connect-redis')(express), + path = require('path'), config = require('../config.js'); (function(app) { @@ -26,8 +27,10 @@ var express = require('express'), // Middlewares app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc) + app.use(express.static(path.join(__dirname, '../', 'public'))); app.use(express.bodyParser()); // Puts POST vars in request.body app.use(express.cookieParser()); // If you want to parse cookies (res.cookies) + app.use(express.compress()); app.use(express.session({ store: new RedisStore({ ttl: 60*60*24*14 @@ -36,9 +39,8 @@ var express = require('express'), key: 'express.sid' })); app.use(function(req, res, next) { - var hasExtension = /\.[\w]{2,4}$/; - if (!hasExtension.test(req.url.indexOf('?') !== -1 ? req.url.substr(0, req.url.indexOf('?')) : req.url)) { - console.log('REQUESTING: ' + req.url); + // Don't bother with session handling for API requests + if (!/^\/api\//.test(req.url)) { if (req.session.uid === undefined) { console.log('info: [Auth] First load, retrieving uid...'); global.modules.user.get_uid_by_session(req.sessionID, function(uid) { @@ -60,11 +62,11 @@ var express = require('express'), // console.log('SESSION: ' + req.sessionID); // console.log('info: [Auth] Ping from uid ' + req.session.uid); } - - // (Re-)register the session as active - global.modules.user.active.register(req.sessionID); } + // (Re-)register the session as active + global.modules.user.active.register(req.sessionID); + next(); }); // Dunno wtf this does @@ -78,8 +80,6 @@ var express = require('express'), }); }); - - app.get('/topics/:topic_id', function(req, res) { global.modules.topics.generate_topic_body(function(topic_body) { res.send(templates['header'] + topic_body + templates['footer']); @@ -140,13 +140,6 @@ var express = require('express'), app.get('/403', function(req, res) { res.send(templates['header'] + templates['403'] + templates['footer']); }); - - module.exports.init = function() { - // todo move some of this stuff into config.json - app.configure(function() { - app.use(express.static(global.configuration.ROOT_DIRECTORY + '/public')); - }); - } }(WebServer)); server.listen(config.port);