Express 4 session TypeError:object不是函数

时间:2015-08-04 19:07:19

标签: node.js session express

我正在使用Express 4学习nodejs来开发一个Web应用程序并关注一些博客,在尝试启动项目时会显示此错误:

[nodetest1]# npm start

nodetest1@0.0.0 start /root/node_proyects/nodetest1
node ./bin/www

/root/node_proyects/nodetest1/app.js:64
app.use(session({secret: 'blablabla'}));
        ^
TypeError: object is not a function
    at Object.<anonymous> (/root/node_proyects/nodetest1/app.js:64:9)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/root/node_proyects/nodetest1/bin/www:7:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

这是app.js文件的负责人:

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(cookieParser());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride()); //**
app.use(passport.initialize()); //**
app.use(passport.session()); //**
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({secret: 'blablabla'}));

和package.json文件,其中模块在项目中加载:

[nodetest1]# cat package.json
{
  "name": "nodetest1",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "express-method-override": "*",
    "express-session": "^1.11.3",
    "jade": "~1.11.0",
    "method-override": "^2.3.5",
    "mongodb": "*",
    "mongoose": "^4.1.1",
    "monk": "~1.0.1",
    "morgan": "~1.6.1",
    "passport": "^0.2.2",
    "passport-facebook": "^2.0.0",
    "serve-favicon": "~2.3.0",
    "session": "^0.1.0"
  }
}

session / Express 4的版本或代码是否有任何问题?

我在谷歌上搜索过,并没有找到任何解决这个问题的线索。

1 个答案:

答案 0 :(得分:4)

您可能希望声明var session = require('express-session');,但在express-session文件中有session和其他名为package.json的内容,您可能会对前者感到困惑。