gpt4 book ai didi

node.js - 引用错误: session is not defined with ('connect-mongo' )(session),

转载 作者:太空宇宙 更新时间:2023-11-04 02:56:51 24 4
gpt4 key购买 nike

我正在尝试添加 connect-mongo为了在nodeJS重新启动时保存 session 。

根据我添加的文档:MongoStore = require('connect-mongo')(session),

并在我的 require('express-session') 中添加了

store: new MongoStore({
url: mongooseDB,
touchAfter: 24 * 3600
})

并注释掉app.use(passport.session());

我收到错误:ReferenceError:未定义 session ,它指向 MongoStore = require('connect-mongo')(session),

我添加了我的 app.js 文件以供引用。我该如何解决这个问题?

'use strict';
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
MongoStore = require('connect-mongo')(session),
passport = require('passport'),
flash = require('connect-flash'),
LocalStrategy = require('passport-local'),
methodOverride = require('method-override'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
User = require('./models/user'),
faker = require('faker'),
path = require('path');

const indexRoute = require('./routes/index'),
usersRoute = require('./routes/users'),
giftsRoute = require('./routes/gifts.js'),
searchRoute = require('./routes/search');

const mongooseDB = 'mongodb://localhost/giftcashing';

mongoose.connect(mongooseDB);

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

app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use(methodOverride("_method"));
app.use(flash());

app.use(require('express-session')({
secret: 'anything',
resave: false,
saveUninitialized: false,
store: new MongoStore({
url: mongooseDB,
touchAfter: 24 * 3600
})
}));

// PASSPORT CONFIGURATION
app.use(passport.initialize());
// app.use(passport.session());

// use static authenticate method of model in LocalStrategy
passport.use(new LocalStrategy(User.authenticate()));

// use static serialize and deserialize of model for passport session support
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function (req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash('error');
res.locals.success = req.flash('success');
next();
});

// 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;
// render the error page
res.status(err.status || 500);
res.render('error');
});

app.use('/', indexRoute);
app.use('/users', usersRoute);
app.use('/gifts', giftsRoute);
app.use('/search', searchRoute);

module.exports = app;

最佳答案

调用时变量session未初始化MongoStore = require('connect-mongo')(session)

在初始化 MongoStore 之前添加 session = require('express-session')

关于node.js - 引用错误: session is not defined with ('connect-mongo' )(session),,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41008645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com