Nikita Zernov
Nikita Zernov

Reputation: 5635

Frontend Redirect in Express.JS

I'm making a little bit complex page using EJS and Express.JS. I'll simplify it a little:

First application loads /update the easy way and renders it:

app.get('/update', (req, res) => {
  res.render('update');
});

update in render function is an EJS template. Here's how it looks like:

...
<div id="progress"></div>
<button id="perform" type="button" class="btn btn-default">Download & Update</button>
<script>
  $('#perform').click(function() {
    $('#perform').hide();
    $('#progress').load('/perform');
  });
</script>
...

As you can see on button click, application loads /perform express.js route into specific div. Here's what it does on Node.JS side:

app.get('/perform', function(req, res) {
  //Renders div
  res.render('perform')
  
  //Does some actions in 1-2 minues.
  
  //Here I want it to redirect to / of application.
});

After it returns some content using res.render(), application starts doing some processes. They take about 1 minute and after completion I want application to redirect to /. I've tried using res.redirect('/') e.t.c., but nothing works. Maybe somehow it should tell browser to redirect..

What can I do here?

Upvotes: 0

Views: 817

Answers (1)

Pieter-Jan Beeckman
Pieter-Jan Beeckman

Reputation: 215

You load "/perform" via ajax, so you can only send a response (which you do with the res.render('perform'))

So you should do the redirect client side

You can do this with polling, a timeout or even better, you could use socket.io to give a signal from server to client when the actions are done.

Upvotes: 1

Related Questions