I found that if middleware in app.js
were in the wrong order , they wouldn't work well.
For example:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); //
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded()); //
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
//the codes below don't work
//set Cookie
app.use(cookieParser());
app.use(session({
secret:setting.cookieSecret,
key: setting.db,
cookie:{maxAge:1000*3600*24*30},
store: new MongoStore({db:setting.db}),
saveUninitialized: true,
resave: true
}) );
//use 'connect-flash'
app.use(flash());
If the code app.use(flash())
isn't moved after app.set('view engine', 'ejs');
, the page will show TypeError: Object #<IncomingMessage> has no method 'flash'
.
What's more,if I just move app.use(flash())
it creates the error req.flash() requires sessions
but the error goes away if I move cookieParse()
and session()
to follow urlencoded()
.
Editing the first code example to this no longer creates errors:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//changed
app.use(flash());
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//changed
app.use(cookieParser());
app.use(session({
secret:setting.cookieSecret,
key: setting.db,
cookie:{maxAge:1000*3600*24*30},
store: new MongoStore({db:setting.db}),
saveUninitialized: true,
resave: true
}) );
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
So I want to know : Is there a mandatory order that the middleware and configuration in app.js must follow?