search

Home  >  Q&A  >  body text

node.js - How to jump to the page after a few seconds after express renders the page?

  1. 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?

女神的闺蜜爱上我女神的闺蜜爱上我2780 days ago1169

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默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/" />

    reply
    0
  • 淡淡烟草味

    淡淡烟草味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().

    reply
    0
  • Cancelreply