I have use case where there needs to be rendering followed by redirection. Rendering is supposed to inform user about successful transaction using inherit view model followed by redirection to home page. Following is desired code:
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');
});
One way is to insert a middleware in next, but that doesn't look the best way to go about it. Please let me know if there is a way to make it happen
As others have said, you can't have both redirection and rendered content when using the 302 status. But, what you can do is to return a 200 status with the rendered content and then have either a <meta> tag or a setTimeout() in the page itself do the redirect. This would show the content briefly (for however much time you want to set it for) and then redirect to a new page.
For example, you could put this in the actual page content so that the browser itself does the redirect after displaying your content:
<script>
setTimeout(function() {
window.location.replace("http://whatever.com/newpage.html")
}, 2000);
</script>
or this:
<meta http-equiv="refresh" content="2;url=http://whatever.com/newpage.html">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With