Got an error while implementing express session
I want to implement session in my app. Here's my code:
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;
But it returned error:
C:UsersUserDesktopmanajemen-event-masternode_modulesexpresslibapplication.js:210 throw new TypeError('app.use() requires middleware functions'); ^
TypeError: app.use() requires middleware functions at EventEmitter.use (C:UsersUserDesktopmanajemen-event-masternode_modulesexpresslibapplication.js:210:11) at Object. (C:UsersUserDesktopmanajemen-event-masterapp.js:45:5) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (C:UsersUserDesktopmanajemen-event-masterbinwww:7:11) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10)
Can you help me how to solve this? Thank you.
您在模块名称中输入了错字。该模块是express-session
,但您的require语句正在加载express-sessions
。
上一篇: 如何以编程方式在iPhone上发送短信?
下一篇: 在执行快速会话时发生错误