我是節點js中的新手,這是我的問題。如何在節點js中使用express重定向後app.get
我設置app.js的代碼如下登錄頁面登錄後重定向,
app.post('/login', function(req, res, next) {
passport.authenticate('local-login', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.flash("email");
var email_address = req.body.email;
var username = email_address.substring(0, email_address.lastIndexOf("@"));
global_username = username;
return res.redirect('/status.html'+'?channel='+username);
})(req, res, next);
});
現在我想「/status.html'+'?channel='+通過使用app.get
用戶名,使我創造了這樣的代碼,
app.get('/status.html?channel='+ global_username, isLoggedIn, function(req, res){
console.log("Logged in");
var chk_username = req.body.email;
var temp = chk_username.substring(0, chk_username.lastIndexOf("@"));
if(temp != global_username){return res.json({success: false, message:"Unauthorized Attempt"});}
});
但是,它不通過app.get功能去..我試圖檢查其是否已登錄或不是通過使用console.log而是沒有出現任何東西。
有沒有人有想法解決這個問題?
謝謝。
後重定向用戶,它結束與服務器的連接,從而防止所有後續路由調用。您可以執行的是執行您在重定向之前的獲取請求期間要執行的代碼。 –
@Theodore_Kim感謝您的評論! :)那麼你的意思是我應該把「app.get('/ status.html?channel ='+ global_username,isLoggedIn,function(req,res){」res.redirect('/ status.html'+'?channel ='+ username);? – paulc1111
不,我的意思是,在重定向客戶端之後,你結束了響應過程,這意味着服務器不會再向服務器發送對原始請求的響應,因此,你不會在重定向客戶端後進入GET路線,我希望這是有道理的...... –