Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why connect-mongo creates new session for every request?

I have two nodejs servers (web-server, socket-server), that are connected to each other by socket.io. On web-service I use express.js and passport.js as authentication middleware.

This is my web-server config:

var express = require('express'),
    mongo = require('mongodb'),
    io = require('socket.io'),
    passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    MongoStore = require('connect-mongo')(express);

app.configure(function () {
    app.use(express.cookieParser());
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(express.session({
        secret: 'keyboard cat',
        store: new MongoStore({
          db: 'MyDatabase'
        })
    }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
    app.use(express.static(__dirname + '/htdocs'));
});

When I use connect-mongo, it creates a new session for each http-request.

This element creates with log in request:

{
  "_id" : "UCnXade6Bk6ofOZ+jiEgzyH8",
  "session" : "{\"cookie\":{\"originalMaxAge\":31536000000,\"expires\":\"2014-03-07T13:07:45.703Z\",\"httpOnly\":true,\"path\":\"/\"},\"passport\":{\"user\":\"50cae08806e31ea2e5634e3f\"}}",
  "expires" : new Date("7.3.2014 19:07:45")
}

And this element creates each time, when I press F5, or take socket.

{
  "_id" : "JhypbYFtj1CGOK/ylMhG8+Yk",
  "session" : "{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{}}",
  "expires" : new Date("21.3.2013 19:03:38")
}

When web-server takes socket connection, connect-mongo creates new session. There are about 50 new documents per minute.

What could be the reason?

UPDATE

In the case of updating the page, helped tip to add app.use(express.favicon()).

With sockets question is still actual.

My socket-server code

function sendPortalJSON (portal_id, data, _event) {
    https.get({
        host : ....,
        port : ....,
        path : "/" + _event + "?data=" + encodeURIComponent( JSON.stringify (data))
    }).on('error', function (err) {

    });
}
...
sendPortalJSON(1, agent_data[i].d, "cpu-details");

And on web-server:

app.get('/cpu-details', function (req, res) { });
like image 650
msmirnov Avatar asked Mar 08 '13 09:03

msmirnov


1 Answers

First, try moving the static middleware to before the session middleware. Also, some browsers handle requests for /favicon.ico a bit funky, so try using express.favicon() to see if it solves your problem.

So something like this:

...
app.use(express.favicon());
app.use(express.static(__dirname + '/htdocs'));
app.use(express.session({...});
...
like image 185
robertklep Avatar answered Nov 15 '22 00:11

robertklep