抛出新的TypeError('app.use()需要中间件功能');会话错误

时间:2018-11-17 20:04:50

标签: node.js express session express-session

因此,我目前正在学习NodeJS,并遇到此错误:

/home/lindan4/Git/HelloNodeJS/ChatTime/node_modules/express/lib/application.js:209
    throw new TypeError('app.use() requires middleware functions');
    ^

TypeError: app.use() requires middleware functions
    at Function.use (/home/lindan4/Git/HelloNodeJS/ChatTime/node_modules/express/lib/application.js:209:11)
    at Object.<anonymous> (/home/lindan4/Git/HelloNodeJS/ChatTime/server.js:14:5)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
    at startup (internal/bootstrap/node.js:308:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:878:3)

这是server.js文件:

'use strict';

const express = require('express');
const app = express();


const chatTime = require('./app');
const passport = require('passport');

app.set('port', process.env.PORT || 3000);
app.use(express.static('public'));

//Below statement must appear before router is mounted
app.use(chatTime.session);
app.use(passport.initialize());
app.use(passport.session());

app.use('/', chatTime.router);
app.set('view engine', 'ejs');



app.listen(app.get('port'), () => {
  console.log('ChatTime running on port', app.get('port'));
});
问题似乎出在“ chatTime.session”行内。它的实现位于下面的文件中:

'use strict';


require('./auth')();


module.exports = {
  router: require('./routes')(),
  session: require('./session')
}

这是“会话”模块的实现:

'use strict';

const session = require('express-session');

//We want to check that the cookie has not been tampered with if the website is reaccessed
const MongoStore = require('connect-mongo')(session);

const config = require('../config');

const db = require('../db');

if (process.env.NODE_ENV === 'production')
{
  //Initialize production settings
  module.exports = session({
      secret: config.sessionSecret,
      resave: false,
      saveUninitialized: false,
      store: new MongoStore({
          mongooseConnection: db.mongoose.connection
      })
    })
}
else
{
  //Initialize development settings

  module.exports = session({
      secret: config.sessionSecret,
      resave: false,
      saveUninitialized: true
    })

}

我似乎无法确切指出需要解决的问题,并希望有人可以提供帮助。

0 个答案:

没有答案