admin 管理员组文章数量: 887021
connect
我在 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 每秒创建一个新会话,正如我从计算数据库中会话集合中的条目数可以看出的那样。即使当前没有活跃用户使用该网站,也会创建会话。这正常吗?
编辑:是否由于我使用中间件检查用户是否使用 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
回答如下:
我也刚刚处理了这个问题,对我来说这是我的 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 为真时,它不会传递任何东西。如果不是,它会正常存储会话。这里的关键部分是
isHealthCheck ||
.
希望这对某人有帮助!
本文标签: connect
版权声明:本文标题:connect 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1712470297h614416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论