I just started learning express and there is a login page. If the verification fails, it will display "The username or password is wrong, please log in again!", and then the page will redirect to the login page after a few seconds, but it has no effect. :
var express = require('express');
var router = express.Router();
router.post('/', function(req, res){
var username = req.body.user;
var password = req.body.password;
if(username === 'liaoyf' && password === '123'){
res.send('Welcome!' + username);
}else{
res.send('用戶名或密碼錯(cuò)誤!請(qǐng)<a href="/">重新登錄</a>');
//這邊我想重定向到登錄頁(yè)(登錄頁(yè)路徑為/)
setTimeout(function(){
res.redirect('/');
}, 2000);
}
});
module.exports = router;
Looking online, it seems that send is completed directly and will not be triggered later. So what should be done?
res.set('refresh', '5;url=http://example.com/');
Or add
in HTML<meta http-equiv="refresh" content="5;url=http://example.com/" />
You can use res.write() for the previous error message. This will not end the response like send() or end().