30

我是表达框架的新手,这是我在server.js文件中的内容:

// 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);
});

当我导航到 localhost:3000 时出现此错误

500 类型错误:无法读取未定义的属性“connect.sid”

问题出在哪里?

4

2 回答 2

76

您缺少cookieParser中间件:

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

(因为会话是使用 cookie 实现的)。

于 2013-03-12T09:30:00.263 回答
1

我遇到了同样的问题。

一定要打电话

app.use( express.cookieParser() );

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

于 2014-12-13T09:03:18.880 回答