实施快速会话时出错

时间:2018-01-20 12:20:01

标签: node.js express

我想在我的应用中实现会话。这是我的代码:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var swig = require('swig');
var session = require('express-sessions');
var helmet = require('helmet');
var login = require('./routes/login');
var edit_profil = require('./routes/manajemen_profil');
var manajemen_venue = require('./routes/manajemen_venue');
var manajemen_akun = require('./routes/manajemen_list_akun');
var manajemen_event = require('./routes/manajemen_event');
var manajemen_list_email = require('./routes/manajemen_list_email');


var app = express();

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

//security reason
app.use(helmet());

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


app.use('/', login);
app.use('/edit_profil', edit_profil);
app.use('/manajemen_venue', manajemen_venue);
app.use('/manajemen_list_akun', manajemen_akun);
app.use('/manajemen_event', manajemen_event);
app.use('/manajemen_list_email', manajemen_list_email);

//Session
app.use(session({
  secret: 'fffdfee',
  resave: false,
  saveUninitialized: false,
}));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('notfound');
});
//test
module.exports = app;

但是它返回了错误:

  

C:\用户\用户\桌面\ manajemen事件主\ node_modules \表达\ lib中\的application.js:210       抛出新的TypeError('app.use()需要中间件函数');       ^

     

TypeError:app.use()需要中间件功能       在EventEmitter.use(C:\ Users \ User \ Desktop \ manajemen-event-master \ node_modules \ express \ lib \ application.js:210:11)       在对象。 (C:\用户\用户\桌面\ manajemen事件主\ app.js:45:5)       在Module._compile(module.js:570:32)       在Object.Module._extensions..js(module.js:579:10)       在Module.load(module.js:487:32)       在tryModuleLoad(module.js:446:12)       在Function.Module._load(module.js:438:3)       在Module.require(module.js:497:17)       at require(internal / module.js:20:19)       在对象。 (C:\用户\用户\桌面\ manajemen事件主\ BIN \ WWW:7:11)       在Module._compile(module.js:570:32)       在Object.Module._extensions..js(module.js:579:10)       在Module.load(module.js:487:32)       在tryModuleLoad(module.js:446:12)       在Function.Module._load(module.js:438:3)       在Module.runMain(module.js:604:10)

你能帮我解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

您在模块名称中输入了拼写错误。该模块为express-session,但您的require语句正在加载express-sessions