Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting weird error in sessions in express framework

I am new to express framework and here is what I have in the server.js file:

// Module dependencies.
var application_root = __dirname,
express = require( 'express' ), //Web framework
path = require( 'path' ), //Utilities for dealing with file paths
mongoose = require( 'mongoose' ); //MongoDB integration

//Create server
var app = express();

// Configure server
app.configure( function() {

app.use( express.bodyParser() );
app.use( express.methodOverride() );
app.use( app.router );
app.use(express.session({secret:'thisismysupersecret'}));
app.use( express.static( path.join( application_root, 'site') ) );
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.post("/verifyLogin",function(request,response){
var usr=request.body.username;
var pass=request.body.password;
//request.session.email=usr;
response.redirect('dashboard');
});

//Start server
var port = 3000;
app.listen( port, function() {
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env);
});

When I navigate to localhost:3000 I get this error

500 TypeError: Cannot read property 'connect.sid' of undefined

Where's the problem?

like image 748
beNerd Avatar asked Mar 12 '13 09:03

beNerd


2 Answers

You're missing the cookieParser middleware:

...
app.use( express.cookieParser() );
app.use(express.session({secret:'thisismysupersecret'}));
...

(since sessions are implemented using cookies).

like image 99
robertklep Avatar answered Oct 15 '22 05:10

robertklep


i was having the same problem.

be sure to calling

app.use( express.cookieParser() );

before

app.use(express.session({secret:'thisismysupersecret'}));

like image 1
user3812138 Avatar answered Oct 15 '22 07:10

user3812138