I'm having some trouble deciphering the ambiguity between
res.render('viewname', {msg: 'Message' })
and
res.redirect('route')
The redirect function does not let you pass a "message", but you may still want to, and the render function will render your view, but it will not change the URL of your web app and will not trigger the function which is required for your route.
The situation I am having is that I have an Invite form, which has an action that changes my URL, and triggers a function at that route which has a success and failure callback, and I would like to redirect users to the Dashboard with a message indicating success or failure.
The res. render() function is used to render a view and sends the rendered HTML string to the client.
-Redirect is a method that is used to issue the error message in case the page is not found or it issues a 302 to the browser. Whereas, render is a method used to create the content. -Redirect is used to tell the browser to issue a new request.
The res. redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below. const app = require('express')(); // The `res.
render() method is used for returning the rendered HTML of a view using the callback function. This method accepts an optional parameter that is an object which contains the local variables for the view.
look at connect-flash to use rails style flashing of messages.
res.render() will render the view with data passed to it, res.redirect() will redirect a user to another page (at which point the request starts over)
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