gpt4 book ai didi

javascript - session 未定义 - 使用 Connect-Redis/ExpressJS/Node

转载 作者:IT王子 更新时间:2023-10-29 05:56:47 25 4
gpt4 key购买 nike

今天早上我醒来发现 Nodejitsu 出现以下错误:

警告:connection.session() MemoryStore 不是为生产环境设计的,因为它会泄漏内存,并且不会扩展到单个进程。

“那么好吧!”我心想 - 然后我去寻找更合适的东西。我选择了 Redis,并立即开始在我的应用程序中实现 connect-redisnode-redis

但是,在反复尝试修复后,以下错误继续浮出水面。错误如下:

TypeError: Cannot set property 'loggedIn' of undefined
at /Users/Ryan/Aggregus/server.js:300:25
at Promise.<anonymous> (/Users/Ryan/Aggregus/object_models/user.js:60:5)
at Promise.<anonymous> (/Users/Ryan/Aggregus/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/Users/Ryan/Aggregus/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/Users/Ryan/Aggregus/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
at /Users/Ryan/Aggregus/node_modules/mongoose/lib/query.js:1813:13
at model.Document.init (/Users/Ryan/Aggregus/node_modules/mongoose/lib/document.js:243:11)
at completeOne (/Users/Ryan/Aggregus/node_modules/mongoose/lib/query.js:1811:10)
at /Users/Ryan/Aggregus/node_modules/mongoose/lib/query.js:1779:11

loggedIn 只是指 req.session.loggedIn,这是我用来跟踪登录用户的 session 变量。以下是有问题的应用程序代码:

var express = require('express');
var crypto = require('crypto');
var mongoose = require('mongoose');
var nodemailer = require('nodemailer');
var request = require('request');
var underscore = require('underscore');
var mandrill = require('node-mandrill')('0bFNPDenlDiZtu7aXujDQQ');
var connect = require('connect');
var engines = require('consolidate');
var fs = require('fs');
var redis = require("redis").createClient();

var RedisStore = require('connect-redis')(express);

var sessionStore = new RedisStore({
client: redis
});

var stripe_api_key = 'KEY';
var Stripe = require('stripe')(stripe_api_key);

var app = express();

app.configure(function() {
app.engine('html', engines.jade);
app.set('view engine', 'html');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({secret: "svtabyrki4q786as37c785ta8vi56aiw4i8w467acv", store: sessionStore}));
app.use(express.static(__dirname + '/www'));
app.use(express.compress());
app.use(app.router);

});

// Mongoose Connection

var db = mongoose.connection;

var dbURI = 'HIDDENURL';

db.on('disconnected', function() {
mongoose.connect(dbURI, {server:{auto_reconnect:true}});
});

db.on('error', function(error) {
console.error('Error in MongoDb connection: ' + error);
mongoose.disconnect();
});

mongoose.connect(dbURI, {server:{auto_reconnect:true}});

// Mongoose Object Models

var User = require('./object_models/user')(mongoose, nodemailer, mandrill);
var Notifications = require('./object_models/notification')(mongoose, nodemailer);
var Experience = require('./object_models/experience')(mongoose, nodemailer);
var Booking = require('./object_models/booking')(mongoose, nodemailer);
var Review = require('./object_models/review')(mongoose, nodemailer);
var Heart = require('./object_models/heart')(mongoose, nodemailer);
var Message = require('./object_models/message')(mongoose, nodemailer);

// Nodemailer Settings

var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "ryan@aggregus.com",
pass: pwd
}
});

// Bootstrap Call

app.get('/', function(req, res) {
res.render('../index.html');
console.log("WUSUP");
});

为了进一步说明,这是我迄今为止尝试过但无济于事的方法:

  1. 我已将 app.use(express.cookieParser()); 放置在 express.session 调用之前,正如该主题的大多数文章所建议的那样。
  2. 我已确保我的 new RedisStore 调用在初始化时直接引用 client
  3. 我在 express.session 和 'express.cookieParser' 的初始化调用之后放置了 app.use(app.router)
  4. 通过 createClient() 调用,我的 Redis 服务器功能齐全。

similar question已通过阻止相当微不足道的 app.use 初始化来在 Express 的 sessionStore 被初始化之前调用 app.router 来解决。

因此,TL;DR:在使用 connect-redis 时,我的 session 变量返回 undefined。我已经在网上搜索了答案,我唯一的线索是 app.router 是在 Express 自己的 session 处理之前启动的。那个小故障发生的地方超出了我的范围。 Stack Overflow 的众神怜悯我可怜的灵魂。

编辑:认为 package.json 可能方便引用。通过 NodeJitsu 生成:

{
"name": "Aggregus",
"subdomain": "aggregus-Aggregus",
"scripts": {
"start": "node server.js"
},
"version": "0.0.0-14",
"engines": {
"node": "0.8.x"
},
"dependencies": {
"connect": "2.7.8",
"consolidate": "0.9.1",
"express": "3.2.2",
"jade": "0.30.0",
"mongoose": "3.6.9",
"node-mandrill":"1.0.1",
"nodemailer": "0.4.1",
"request":"2.22.0",
"stripe": "1.3.0",
"underscore":"1.4.4"
}
}

最佳答案

你调用 connect-redis 是错误的,你不需要单独安装 redis。但是,我在你的 package.json 中没有看到 connect-redis,所以你应该 npm install --save connect-redis。然后做这样的事情:

var express = require('express');
var RedisStore = require('connect-redis')(express);
var ports = require('./classes/ports.js');
var config = require('./config/config.js');
var routes = require('./routes');
var errors = require('./classes/errors.js');

var app = express();
ports(app);
app
.use(express.logger())
.use(express.compress())
.use(express.cookieParser())
.use(express.session({
store: new RedisStore({
port: config.redisPort,
host: config.redisHost,
db: config.redisDatabase,
pass: config.redisPassword
}),
secret: 'Your secret here',
proxy: true,
cookie: { secure: true }
}))
.use(express.favicon(__dirname + '/../public/img/favicon.ico'))
.use(express.bodyParser())
.use(express.methodOverride())

routes(app);
errors(app);

关于javascript - session 未定义 - 使用 Connect-Redis/ExpressJS/Node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17600272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com