req.flash()需要会话,但会话已经存在

时间:2018-07-26 23:27:49

标签: node.js express

我正在尝试打一条简单的/路线,并使用res.send()确保它可以正常工作。我得到req.flash() requires sessions,但已经使用Redis实例设置了会话。

我在Stack溢出上花费了大约2个小时,试图找到解决方案,我尝试了很多事情,但没有任何效果。除非您确定是解决方案,否则请不要链接到重复的帖子。

App.js

const   express = require("express"),
        app = express(),
        mongoose = require("mongoose"),
        session = require('express-session'),
        flash = require('express-flash'),
        RedisStore = require('connect-redis')(session),
        bodyParser  = require("body-parser"),
        passport    = require("passport"),
        LocalStrategy = require("passport-local"),
        methodOverride = require("method-override");       

// Models
const User = require("./models/user");

// Configuration
const config = require("./config/config");

mongoose.connect(config.db);
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
// set session
app.use(session({
  resave: true,
  saveUninitialized: false,
  cookie: {
    maxAge: 60 * 1000 * 60 * 24 * 30 // 30 days
  },
  secret: config.sessionSecret,
  store: new RedisStore({
    url: "redis://localhost/sail",
    auto_reconnect: true
  })
}));
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());

// PASSPORT CONFIGURATION


app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());


app.get('/', (req, res) => {
    res.send('hello world');
});


module.exports = app;

0 个答案:

没有答案