【问题标题】:Frontend Redirect in Express.JSExpress.JS 中的前端重定向
【发布时间】:2017-11-08 11:30:40
【问题描述】:

我正在使用EJSExpress.JS 制作一个有点复杂的页面。我稍微简化一下:

第一个应用程序以简单的方式加载 /update 并呈现它:

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

render 函数中的update 是一个EJS 模板。下面是它的样子:

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

正如您在单击按钮时看到的那样,应用程序加载 /perform express.js 路由到特定的 div。这是它在Node.JS 方面的作用:

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.
});

在它使用res.render() 返回一些内容后,应用程序开始执行一些进程。它们大约需要 1 分钟,完成后我希望应用程序重定向到 /。我试过使用res.redirect('/') e.t.c.,但没有任何效果。也许它应该以某种方式告诉浏览器重定向..

我可以在这里做什么?

【问题讨论】:

    标签: javascript jquery node.js redirect


    【解决方案1】:

    您通过 ajax 加载“/perform”,因此您只能发送响应(使用 res.render('perform'))

    所以你应该做重定向客户端

    您可以通过轮询、超时甚至更好的方式来做到这一点,您可以使用 socket.io 在操作完成时从服务器向客户端发出信号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多