我不清楚如何顺序调用 Express 中间件。我希望下一个中间件只有在前一个完成后才会发生。我以为我必须调用 next() 才能发生,但显然情况并非如此。
mongoose.connect(app.set("db-uri"));
app.use(function(req, res, next) {
if(1 !== mongoose.connection.readyState) {
console.log('Database not connected');
res.render("system/maintenance", {
status: 500
});
} else {
return next();
}
});
// This middleware does not wait for the previous next(). It just tries to connect before actually.
app.use(express.session({
secret: settings.sessionSecret,
maxAge: new Date(Date.now() + 3600000),
store: new MongoStore({ mongoose_connection: mongoose.connections[0], auto_reconnect: true })
}));
编辑:更新代码以仅在启动时连接 Mongoose 并检查中间件中的状态