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('用户名或密码错误!请<a href="/">重新登录</a>');
//这边我想重定向到登录页(登录页路径为/)
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?
習慣沉默2017-06-24 09:45:30
res.set('refresh', '5;url=http://example.com/');
Or add
in HTML<meta http-equiv="refresh" content="5;url=http://example.com/" />
淡淡烟草味2017-06-24 09:45:30
You can use res.write() for the previous error message. This will not end the response like send() or end().