带有connect-mysql-session的Expressjs

时间:2011-12-08 03:06:14

标签: javascript node.js express nowjs-sockets

我正在使用带有expressjs的connect-mysql-session模块。对于服务器的每个请求,我得到:

Express server listening on port 3000 in development mode
Executing: CREATE TABLE IF NOT EXISTS `Sessions` (`sid` VARCHAR(255), `expires` INT, `json` TEXT, `id` INT NOT NULL auto_increment , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
MySQL session store initialized.
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: UPDATE `Sessions` SET `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo',`expires`=1323327347,`json`='{\"lastAccess\":1323312946989,\"cookie\":{\"originalMaxAge\":14400000,\"expires\":\"2011-12-08T06:55:47.013Z\",\"httpOnly\":true,\"path\":\"/\"},\"status\":true,\"_csrf\":\"kzvoTUf88Nr36YR2g7w6Y9P8\"}',`id`=9,`createdAt`='2011-12-08 03:52:11',`updatedAt`='2011-12-08 03:55:47' WHERE `id`=9
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: UPDATE `Sessions` SET `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo',`expires`=1323327347,`json`='{\"lastAccess\":1323312947060,\"cookie\":{\"originalMaxAge\":14400000,\"expires\":\"2011-12-08T06:55:47.061Z\",\"httpOnly\":true,\"path\":\"/\"},\"status\":true,\"_csrf\":\"kzvoTUf88Nr36YR2g7w6Y9P8\"}',`id`=9,`createdAt`='2011-12-08 03:52:11',`updatedAt`='2011-12-08 03:55:47' WHERE `id`=9
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: UPDATE `Sessions` SET `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo',`expires`=1323327347,`json`='{\"lastAccess\":1323312947088,\"cookie\":{\"originalMaxAge\":14400000,\"expires\":\"2011-12-08T06:55:47.089Z\",\"httpOnly\":true,\"path\":\"/\"},\"status\":true,\"_csrf\":\"kzvoTUf88Nr36YR2g7w6Y9P8\"}',`id`=9,`createdAt`='2011-12-08 03:52:11',`updatedAt`='2011-12-08 03:55:47' WHERE `id`=9
Executing: UPDATE `Sessions` SET `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo',`expires`=1323327347,`json`='{\"lastAccess\":1323312947089,\"cookie\":{\"originalMaxAge\":14400000,\"expires\":\"2011-12-08T06:55:47.090Z\",\"httpOnly\":true,\"path\":\"/\"},\"status\":true,\"_csrf\":\"kzvoTUf88Nr36YR2g7w6Y9P8\"}',`id`=9,`createdAt`='2011-12-08 03:52:11',`updatedAt`='2011-12-08 03:55:47' WHERE `id`=9
Executing: UPDATE `Sessions` SET `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo',`expires`=1323327347,`json`='{\"lastAccess\":1323312947114,\"cookie\":{\"originalMaxAge\":14400000,\"expires\":\"2011-12-08T06:55:47.115Z\",\"httpOnly\":true,\"path\":\"/\"},\"status\":true,\"_csrf\":\"kzvoTUf88Nr36YR2g7w6Y9P8\"}',`id`=9,`createdAt`='2011-12-08 03:52:11',`updatedAt`='2011-12-08 03:55:47' WHERE `id`=9
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: SELECT * FROM `Sessions` WHERE `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo' LIMIT 1;
Executing: UPDATE `Sessions` SET `sid`='4SQXzLCo1O44CLMdrA1WjVVC.lecbAd5SOXHkyJfR+YEpIVJ8p9E8m79TAGGTwxnoCuo',`expires`=1323327347,`json`='{\"lastAccess\":1323312947253,\"cookie\":{\"originalMaxAge\":14400000,\"expires\":\"2011-12-08T06:55:47.254Z\",\"httpOnly\":true,\"path\":\"/\"},\"status\":true,\"_csrf\":\"kzvoTUf88Nr36YR2g7w6Y9P8\"}',`id`=9,`createdAt`='2011-12-08 03:52:11',`updatedAt`='2011-12-08 03:55:47' WHERE `id`=9

为什么要这样做?

我为每个请求设置了大约8个cookie(connect.sid),我认为它是为每个资产设置的,包括js,css和图像。它为什么这么多次设定?它是否被假定?

继承我的配置:

app.set('views', __dirname + '/../views');
app.set('view engine', 'html');
app.register('.html', require('ejs'));
app.use(express.bodyParser());
app.use(expressValidator);
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
store: new MySQLSessionStore("test_db", "root", "root"), 
    secret: 'cat'
}));
app.use(app.router);
app.use(express.csrf());
app.use(express.static(__dirname + '/../public'));

此致

1 个答案:

答案 0 :(得分:1)

我对此并不是100%肯定,但我认为你应该把静态中间件放在首位。重点是上面有静态中间件,因为这不需要会话,cookie和其他中间件,如下所示:

app.set('views', __dirname + '/../views');
app.set('view engine', 'html');
app.register('.html', require('ejs'));
app.use(express.static(__dirname + '/../public'));
app.use(express.bodyParser());
app.use(expressValidator);
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
store: new MySQLSessionStore("test_db", "root", "root"), 
    secret: 'cat'
}));
app.use(app.router);
app.use(express.csrf());