为什么命令顺序会改变代码的行为?

时间:2019-07-04 16:47:20

标签: node.js express session passport.js

app.set('view engine','ejs');
mongoose.connect("mongodb://localhost/socialnetwork");
app.use(bodyParser.urlencoded({extended:true}));
app.use(require("express-session")({
    secret:"xxxx",//Used to encode/decode session;
    resave:false,
    saveUninitialized:false
}));
// Next 2 lines are needed anytime passport is used
app.use(passport.initialize());
app.use(passport.session());
//These methods are responsile for reading the session and encoding/decoding the session data
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

此代码可以正常工作,并且符合预期,但是无论如何,以下代码都不会对用户登录进行身份验证。

app.set('view engine','ejs');
mongoose.connect("mongodb://localhost/socialnetwork");
// Next 2 lines are needed anytime passport is used
app.use(passport.initialize());
app.use(passport.session());

app.use(bodyParser.urlencoded({extended:true}));
app.use(require("express-session")({
    secret:"xxxx",//Used to encode/decode session;
    resave:false,
    saveUninitialized:false
}));
//These methods are responsile for reading the session and encoding/decoding the session data
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

我唯一更改的是将app.use(passport.initialize());app.use(passport.session());命令向下移动到“ express-session”要求命令的下方。

有人可以解释这种行为还是指向可以做到这一点的资源?

0 个答案:

没有答案