techpines / express.io

Realtime Micro Framework for Nodejs
express-io.org
1.59k stars 234 forks source link

cookieParser breaks when a page reloads #102

Open bpytlik opened 10 years ago

bpytlik commented 10 years ago

After fixing issue #99 I discovered that when I reloaded my page, I got the following stack trace from express.io:

/node_modules/cookie-parser/index.js:25 if (req.cookies) return next(); ^ TypeError: Cannot read property 'cookies' of undefined at cookieParser (/node_modules/cookie-parser/index.js:25:12) at /node_modules/express.io/lib/index.coffee:138:24 at Manager.app_io_opts.gen_auth_middleware_f (/httpd/server.coffee:450:9) at Manager.authorize (/node_modules/express.io/node_modules/socket.io/lib/manager.js:925:31) at Manager.handleHandshake (/node_modules/express.io/node_modules/socket.io/lib/manager.js:801:8) at Manager.handleRequest (/node_modules/express.io/node_modules/socket.io/lib/manager.js:616:12) at Server. (/node_modules/express.io/node_modules/socket.io/lib/manager.js:119:10) at Server.EventEmitter.emit (events.js:98:17) at HTTPParser.parser.onIncoming (http.js:2108:12) at HTTPParser.parserOnHeadersComplete [as onHeadersComplete](http.js:121:23)

This happens because at the top of index.coffee, cookieParser is defined like this: cookieParser = require 'cookie-parser'

Down below, we see this code: cookieParser = cookieParser()

So the first time we come through, we call the cookieParser factory function (which takes a secret and options) and then set the result to be cookieParser. The result is a middleware function that expects 3 arguments, req, res, and next.

The next time we enter the authorization function (because of a page refresh for example), we again do cookieParser = cookieParser(). However, cookieParser is now the middleware function expecting 3 arguments. Since we pass it non, we get the stack trace seen above.