Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 是否可以在express+;中的渲染之后进行重定向;节点js?_Node.js_Express - Fatal编程技术网

Node.js 是否可以在express+;中的渲染之后进行重定向;节点js?

Node.js 是否可以在express+;中的渲染之后进行重定向;节点js?,node.js,express,Node.js,Express,我有一个用例,其中需要进行渲染,然后进行重定向。渲染应该使用继承视图模型通知用户事务成功,然后重定向到主页。以下是所需代码: router.post('/posturi', function(req, res, next) { //Call few services if(err){ return res.render('/errorpage') } res.render('/transaction page'); //TimeOut function

我有一个用例,其中需要进行渲染,然后进行重定向。渲染应该使用继承视图模型通知用户事务成功,然后重定向到主页。以下是所需代码:

router.post('/posturi', function(req, res, next) {
   //Call few services
   if(err){ 
     return res.render('/errorpage')
   }
   res.render('/transaction page');
   //TimeOut function goes here
   res.redirect('/home');
});

一种方法是在next中插入中间件,但这看起来不是最好的方法。请让我知道是否有办法实现它

否。这是因为
重定向
渲染
都会发送标题并完成响应。发送内容后不能发送标题。

正如其他人所说,使用302状态时不能同时重定向和呈现内容。但是,您可以使用呈现的内容返回200状态,然后在页面本身中使用
标记或
setTimeout()
执行重定向。这将简短地显示内容(无论您希望设置多长时间),然后重定向到新页面

例如,您可以将其放入实际页面内容中,以便浏览器本身在显示内容后执行重定向:

<script>
setTimeout(function() {
    window.location.replace("http://whatever.com/newpage.html")
}, 2000);
</script>

setTimeout(函数(){
window.location.replace(“http://whatever.com/newpage.html")
}, 2000);
或者这个:

<meta http-equiv="refresh" content="2;url=http://whatever.com/newpage.html">


您可以使用
setTimeout
在客户机上执行此操作(与无法调用渲染两次的原因相同)