How to add timeout function to a response method after the root file is loaded in express js

Viewed 46

I want to use a setTimeout function to make a redirection after the root file has been displayed in express js example:

app.get('/', (req,res)=>{
    res.render('index');
    setTimeout(()=>{
     res.render('homepage');
   },3000)
  }

but it seems not to work

1 Answers

You can only render one file. If you want to redirect after it has been displayed, your index file should have some javascript, such as:

<script>

setTimeout(() => {
  location.pathname = '/homepage';
}, 3000);

</script>
Related