快速创建大量会话

时间:2017-09-20 15:07:20

标签: node.js session express

我只是意识到这种情况已经发生了。

我的筹码:

  • 的NodeJS
  • 快速
  • PassportJS
  • 使用Mongoose的MongoDB

当我登录我的网站时,我发现创建了集合会话,其中包含一条记录(正如人们所期望的那样)。然后,我在网站上浏览并注意到我的会话已经转到13条记录。然后,我再浏览一下,我发现它已超过27个。

世界上发生了什么?我在做一些导致这种情况的事情吗?

我的主要app.js文件如下:

var config = require('./config/config.js');
var express = require('express');
var favicon = require('serve-favicon')
var app = express();
var path = require('path');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var ConnectMongo = require('connect-mongo')(session);
var mongoose = require('mongoose').connect(config.dbURL);
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var bodyParser = require('body-parser');

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('hogan-express'));
app.set('view engine', 'html');
app.use(favicon(path.join(__dirname, 'public', '/images/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(session({
    secret:config.sessionSecret,
    store: new ConnectMongo({
        mongooseConnection:mongoose.connections[0],
        stringigy:true,
        touchAfter: 24 * 3600
    }),
    saveUninitialized:true,
    resave:true
}));

app.use(session({secret:config.sessionSecret, saveUninitialized:true, resave:true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

require('./schemas/allSchemas.js')(mongoose);
require('./auth/passportAuth.js')(passport, FacebookStrategy, config, mongoose, moment);

//Main URL router
require('./routes/routes.js')(express, app, passport, mongoose);

app.listen(3000, function(){
    console.log('App is working on Port 3000');
});

我的每个会话都如下所示(如果它有任何帮助):

{
"_id": "cSqLjOGJlYBvNLYJ8v6zXqtqzuIcGwbx",
"session": "{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"path\":\"/\"}}",
"expires": "2017-10-10 22:46:09",
"lastModified": "2017-09-26 22:46:09"
}

1 个答案:

答案 0 :(得分:1)

试试这个

var config = require('./config/config.js');
var express = require('express');
var favicon = require('serve-favicon')
var app = express();
var path = require('path');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var ConnectMongo = require('connect-mongo')(session);
var mongoose = require('mongoose').connect(config.dbURL);
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var bodyParser = require('body-parser');

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('hogan-express'));
app.set('view engine', 'html');
app.use(favicon(path.join(__dirname, 'public', '/images/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
    secret:config.sessionSecret,
    store: new ConnectMongo({
        mongooseConnection:mongoose.connections[0],
        stringigy:true,
        touchAfter: 24 * 3600
    }),
    saveUninitialized:true,
    resave:true
}));

app.use(passport.initialize());
app.use(passport.session());


require('./schemas/allSchemas.js')(mongoose);
require('./auth/passportAuth.js')(passport, FacebookStrategy, config, mongoose, moment);

//Main URL router
require('./routes/routes.js')(express, app, passport, mongoose);

app.listen(3000, function(){
    console.log('App is working on Port 3000');
});