connect-mongo 每秒创建一个新会话
connect-mongo creating new session every second
我在 Openshift 上托管了我的 nodejs 应用程序。这是我的规格:
node v0.10.35, express v3.4.8
我的 package.json 依赖项:
"dependencies": {
"angular-loading-bar": "^0.9.0",
"async": "^2.0.0-rc.5",
"bcrypt-nodejs": "0.0.3",
"body-parser": "~1.0.0",
"connect-flash": "^0.1.1",
"connect-mongo": "^1.2.0",
"cookie-parser": "~1.0.0",
"ejs": "^2.4.1",
"express": "~3.4.4",
"lodash": "^4.12.0",
"method-override": "~1.0.0",
"mongodb": "~2.x",
"mongoose": "~4.4.12",
"morgan": "~1.0.0",
"nodemailer": "^2.3.2",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"recaptcha2": "^1.0.8"
},
这是我的 server.js
#!/bin/env node
var express = require('express');
var fs = require('fs');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var MongoStore = require('connect-mongo/es5')(express);
var app = express();
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
//MongoD
mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + "tenders";
mongoose.connect(mongodb_connection_string);
var dbconn = mongoose.connection;
dbconn.on('error', console.error.bind(console, 'connection error:'));
dbconn.once('open', function(){
console.log('Connected to Mongoose Database.');
});
// Close MongoD connection when app is terminated
process.on('SIGINT', function (){
mongoose.disconnect();
dbconn.close(function (){
console.log("Server halted: Mongoose default connection disconnected.");
process.exit(0);
});
});
/* Configuration */
app.set('view engine', 'ejs'); // set up ejs for templating
/* Middlewares */
app.use(express.static(__dirname + "/views"));
app.use(express.static(__dirname + "/public"));
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser()); // get information from html forms
app.use(cookieParser()); // read cookies (needed for auth)
/** Persistent database backed session **/
app.use(express.session({
secret: process.env.SECRET,
store: new MongoStore({mongooseConnection : mongoose.connection})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./routes/routes')(app, passport);
require('./config/passport')(passport); configuration
app.use(function(req, res) {
res.redirect('/')
});
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401);
res.json({"message" : err.name + ": " + err.message});
}
});
/* Start server */
app.listen(server_port, server_ip_address, function(){
console.log("Listening on " + server_ip_address + ":" + server_port);
});
问题是 connect-mongo 正在每秒创建一个新会话,正如我从计算数据库中会话集合中的条目数可以看出的那样。即使当前没有活跃用户使用该网站,也会创建会话。这正常吗?
Edit :是否是由于我使用中间件检查用户是否使用 passport isauthenticated 方法在大多数 api 调用中登录。但奇怪的是,即使没有用户向服务器发出请求,它也会被调用,正如你从下面的 node.log 中看到的那样,它一直在重定向
GET / 302 3ms - 40b
GET / 302 3ms - 40b
GET / 302 2ms - 40b
GET / 302 2ms - 40b
GET / 302 3ms - 40b
GET / 302 20ms - 40b
GET / 302 3ms - 40b
GET / 302 2ms - 40b
GET / 302 3ms - 40b
GET / 302 4ms - 40b
如果您使用的是 express-session >= 1.10.0 并且不想在每次用户刷新页面时都在数据库中重新保存所有会话,您可以延迟更新会话,通过限制一个时间段的时间。
因为您使用的是更新版本的 connect-mongo 和旧版本的 express 我不是 100% 确定,但我认为这是因为 cookie 或未初始化的会话。
// Configuring sessions
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: 'JohnSecret',
saveUninitialized: false, // don't create session until something stored
resave: false, //don't save session if unmodified
store: new MongoStore({
url: 'mongodb://localhost/John',
autoRemove: 'interval',
autoRemoveInterval: 10 // In minutes. Default
})
}));
好的,这是 HAProxy 不断检查后端服务器以查看其是否正常运行的问题。这样做会在一秒钟内创建一个会话并弄乱我的数据库。所以这是我的 (dirty) 修复:
创建一个 api /ping
通过销毁每个会话来处理 HAProxy 的 httpchk
app.get('/ping', function(req, res){
req.session.destroy();
res.send(200);
});
配置haproxy/conf
将option httpchk GET /
改为option httpchk GET /ping
使用 RHC 重启 HAProxy cartridge rhc cartridge-restart --cartridge haproxy
我也刚刚处理了这个问题,对我来说这是我的 AWS 健康检查的结果。每个 healthcheck ping 都会创建并存储一个新会话。
当请求是健康检查时,我通过绕过商店来解决这个问题:
app.use(function(req, res, done) {
var isHealthcheck = req.url.indexOf('healthcheck') > -1;
session({
secret: config.secrets.session,
saveUninitialized: true,
resave: false,
store: isHealthcheck || new MongoStore({
mongooseConnection: mongoose.connection,
db: 'myDb'
})
})(req, res, done);
});
因此,当 isHealthcheck 为 true 时,它不会传递任何内容。如果不是,它会正常存储会话。这里的关键部分是isHealthCheck ||
.
希望这对某人有所帮助!
我在 Openshift 上托管了我的 nodejs 应用程序。这是我的规格:
node v0.10.35, express v3.4.8
我的 package.json 依赖项:
"dependencies": {
"angular-loading-bar": "^0.9.0",
"async": "^2.0.0-rc.5",
"bcrypt-nodejs": "0.0.3",
"body-parser": "~1.0.0",
"connect-flash": "^0.1.1",
"connect-mongo": "^1.2.0",
"cookie-parser": "~1.0.0",
"ejs": "^2.4.1",
"express": "~3.4.4",
"lodash": "^4.12.0",
"method-override": "~1.0.0",
"mongodb": "~2.x",
"mongoose": "~4.4.12",
"morgan": "~1.0.0",
"nodemailer": "^2.3.2",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"recaptcha2": "^1.0.8"
},
这是我的 server.js
#!/bin/env node
var express = require('express');
var fs = require('fs');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var MongoStore = require('connect-mongo/es5')(express);
var app = express();
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
//MongoD
mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + "tenders";
mongoose.connect(mongodb_connection_string);
var dbconn = mongoose.connection;
dbconn.on('error', console.error.bind(console, 'connection error:'));
dbconn.once('open', function(){
console.log('Connected to Mongoose Database.');
});
// Close MongoD connection when app is terminated
process.on('SIGINT', function (){
mongoose.disconnect();
dbconn.close(function (){
console.log("Server halted: Mongoose default connection disconnected.");
process.exit(0);
});
});
/* Configuration */
app.set('view engine', 'ejs'); // set up ejs for templating
/* Middlewares */
app.use(express.static(__dirname + "/views"));
app.use(express.static(__dirname + "/public"));
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser()); // get information from html forms
app.use(cookieParser()); // read cookies (needed for auth)
/** Persistent database backed session **/
app.use(express.session({
secret: process.env.SECRET,
store: new MongoStore({mongooseConnection : mongoose.connection})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require('./routes/routes')(app, passport);
require('./config/passport')(passport); configuration
app.use(function(req, res) {
res.redirect('/')
});
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401);
res.json({"message" : err.name + ": " + err.message});
}
});
/* Start server */
app.listen(server_port, server_ip_address, function(){
console.log("Listening on " + server_ip_address + ":" + server_port);
});
问题是 connect-mongo 正在每秒创建一个新会话,正如我从计算数据库中会话集合中的条目数可以看出的那样。即使当前没有活跃用户使用该网站,也会创建会话。这正常吗?
Edit :是否是由于我使用中间件检查用户是否使用 passport isauthenticated 方法在大多数 api 调用中登录。但奇怪的是,即使没有用户向服务器发出请求,它也会被调用,正如你从下面的 node.log 中看到的那样,它一直在重定向
GET / 302 3ms - 40b
GET / 302 3ms - 40b
GET / 302 2ms - 40b
GET / 302 2ms - 40b
GET / 302 3ms - 40b
GET / 302 20ms - 40b
GET / 302 3ms - 40b
GET / 302 2ms - 40b
GET / 302 3ms - 40b
GET / 302 4ms - 40b
如果您使用的是 express-session >= 1.10.0 并且不想在每次用户刷新页面时都在数据库中重新保存所有会话,您可以延迟更新会话,通过限制一个时间段的时间。 因为您使用的是更新版本的 connect-mongo 和旧版本的 express 我不是 100% 确定,但我认为这是因为 cookie 或未初始化的会话。
// Configuring sessions
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: 'JohnSecret',
saveUninitialized: false, // don't create session until something stored
resave: false, //don't save session if unmodified
store: new MongoStore({
url: 'mongodb://localhost/John',
autoRemove: 'interval',
autoRemoveInterval: 10 // In minutes. Default
})
}));
好的,这是 HAProxy 不断检查后端服务器以查看其是否正常运行的问题。这样做会在一秒钟内创建一个会话并弄乱我的数据库。所以这是我的 (dirty) 修复:
创建一个 api
/ping
通过销毁每个会话来处理 HAProxy 的 httpchkapp.get('/ping', function(req, res){ req.session.destroy(); res.send(200); });
配置
haproxy/conf
将option httpchk GET /
改为option httpchk GET /ping
使用 RHC 重启 HAProxy cartridge
rhc cartridge-restart --cartridge haproxy
我也刚刚处理了这个问题,对我来说这是我的 AWS 健康检查的结果。每个 healthcheck ping 都会创建并存储一个新会话。 当请求是健康检查时,我通过绕过商店来解决这个问题:
app.use(function(req, res, done) {
var isHealthcheck = req.url.indexOf('healthcheck') > -1;
session({
secret: config.secrets.session,
saveUninitialized: true,
resave: false,
store: isHealthcheck || new MongoStore({
mongooseConnection: mongoose.connection,
db: 'myDb'
})
})(req, res, done);
});
因此,当 isHealthcheck 为 true 时,它不会传递任何内容。如果不是,它会正常存储会话。这里的关键部分是isHealthCheck ||
.
希望这对某人有所帮助!