码迷,mamicode.com
首页 > Web开发 > 详细

Cannot read property 'Store' of undefined nodejs express session

时间:2015-02-19 23:02:59      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:nodejs express sessi   store of undefined   nodejs session   express 4 session   session   

Express在使用mongodb的时候app配置出错!  "Cannot read property ‘Store‘ of undefined"

原因主要是express版本4++问题


//settings.js

module.exports={

   cookieSecret:"xxxx",

   db:"dbname",

   host:"localhost",

}


//app.js

var express = require("express");

var settings = require("./settings");


加载connect-mongo的出错写法:

var MongoStore=require("connect-mongo")(express);

app.use(express.session({
    secret:Settings.cookieSecret,
    key:Settings.db,
    cookie:{maxAge:1000*60*60*24*30},//30 days
    store:new MongoStore({
        db:Settings.db
    })
}));

启动后报错:TypeError: Cannot read property ‘Store‘ of undefined

以上是 express<4的写法


express 4.0之后(包括4.0)写法如下:

var session = require("express-session");

var MongoStore=require("connect-mongo")(session);

app.use(session({

   secret:settings.cookieSecret,

   store:new MongoStore({

      db:settings.db

   })

});


package.json配置如下

{
  "name": "dbTest",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "express": "~4.2.0",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "ejs": "~0.8.5",
    "mongodb":"*",
    "connect-mongo":"*",
    "express-session":"*"
  }
}


Cannot read property 'Store' of undefined nodejs express session

标签:nodejs express sessi   store of undefined   nodejs session   express 4 session   session   

原文地址:http://blog.csdn.net/zw200813ht/article/details/43883981

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!