7

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

Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process.

'好吧!' 我心想——然后我去寻找更合适的东西。我选择了 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,我用来跟踪登录用户的会话变量。以下是有问题的应用程序代码:

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. 我已经app.use(app.router)在两个初始化调用express.session和'express.cookieParser'之后放置了。
  4. 通过调用,我的 Redis 服务器功能齐全createClient()

通过防止在 Express 的 sessionStore 被初始化之前调用一个相当微不足道的初始化来解决这个类似的问题。app.useapp.router

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

编辑:认为 package.json 可能便于参考。通过 NodeJits 生成:

{
  "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"
  }
}
4

1 回答 1

8

你调用 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);
于 2013-07-12T03:00:08.667 回答