Why does connect-mongo create a new session for each request?

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

This is my web server configuration:

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 item is created with a log 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 is created every time I press F5 or accept a 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 the web server accepts the socket connection, connect-mongo creates a new session. About 50 new documents per minute.

What could be the reason?

UPDATE

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

The issue with sockets is still relevant.

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 the web server:

 app.get('/cpu-details', function (req, res) { }); 
+7
source share
1 answer

First, try moving the static middleware to the session middleware. In addition, some browsers handle requests for /favicon.ico 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({...}); ... 
+2
source

All Articles