2
votes

I'm using expressjs and [email protected] as the MongoStore for session.

What happen is, the session code was working. but after I upgrade node and npm to the latest version, and Mongo to 2.0.2, then if I put express.session ( store:new MongoStore) then it will run a infinite loop. Any idea what's happening?

here is my code:

express.createServer(
  express.cookieParser(),
  express.bodyParser(),
  express.session({ secret: cfg.session_secret,
  cookie: { domain: 'mydomain.com' },
  store:new MongoStore({

    db: cfg.db_session_name,
    host: cfg.db_ip,
    port: cfg.db_port
    })
  })
)

Here is the error:

TypeError: Not a string or buffer
at Object.createHmac (crypto.js:129:21)
at Object.sign (/node_modules/connect-mongo/node_modules/connect/lib/utils.js:135:6)
at Object.serialize (/node_modules/connect-mongo/node_modules/connect/lib/middleware/session/cookie.js:115:17)
at ServerResponse.writeHead (/node_modules/express/node_modules/connect/lib/middleware/session.js:265:46)
at ServerResponse._implicitHeader (http.js:808:8)
at ServerResponse.end (http.js:645:10)
at next (/node_modules/express/node_modules/connect/lib/http.js:167:13)
at pass (/node_modules/express/lib/router/index.js:219:24)
at nextRoute (/node_modules/express/lib/router/index.js:209:7)
at callbacks (/node_modules/express/lib/router/index.js:274:11)
2
I'm confused, you said you get an infinite loop, but also gave an unrelated error message? - loganfsmyth
it will just sending the error message forever,. keep sending out. - murvinlai
What's your Connect version? I believe this was updated very recently and it may lack some backwards compatibility. - Gates VP

2 Answers

4
votes

Just got the temp solution for that. from another noder "jacobstr"

see this: https://github.com/kcbanner/connect-mongo/issues/29#issuecomment-4233108

So, I go to node_modules/connect-mongo folder. Edit the package.json like that: https://github.com/jacobstr/connect-mongo/blob/master/package.json (only one change: <2 ).

Then in that folder, run sudo npm install -d

then everything works. :)

2
votes

I ran into the same issue. I believe this is being caused by express and connect-mongo using different versions of connect and those different versions have different method signatures for the cookieParser. The version connect-mongo is using expects to be passed a "secret" key for signing the session data, but the version express uses does not supply it.

There is an update to express on GitHub to support connect 2.0, but it is not available via npm yet.