1
我的觀點依賴於一些數據來呈現,就像這樣:如何在res.redirect期間傳遞數據?
router.get("/", (req, res) => {
res.render(
'index', {
data: layout_data,
user: req.user
}
);
});
router.post('/login', passport.authenticate('local'), function(req, res) {
res.redirect('/');
});
它的工作原理。但是,當我加入了csurf中間件像
router.post('/login', parseForm, csrfProtection, passport.authenticate('local'), function(req, res) {
res.redirect('/');
});
佈局引擎告訴我,data
是不確定的。我懷疑csurf中間件會擦除數據。
我試圖修復它是這樣的:
router.get("/", (req, res) => {
req.mydata = {};
req.mydata.layout_data = layout_data;
res.render(
'index', {
data: req.mydata.layout_data,
user: req.user
}
);
});
router.post('/login', parseForm, csrfProtection, passport.authenticate('local'), function(req, res) {
if (!req.mydata) {
req.mydata = {};
}
req.mydata.layout_data = layout_data;
res.redirect('/');
});
它仍然無法正常工作。數據仍未定義。
解決此問題的最佳做法是什麼?
當你重定向一個URL時,它會變成一個GET請求。在GET請求中,您需要使用URL查詢。 – Paras
@Paras,但是當我從中間件中刪除'csrfProtection'時,它可以工作。 – Colliot