redirect in a form submit

1,339 views
Skip to first unread message

Angelo Chen

unread,
Feb 8, 2012, 9:14:23 PM2/8/12
to Express
Hi,

this is a route for a form submit, after processing, a feedback is
returned, problem is, the url is not changed,
still http://localhost:8080/photo_update_submit, what is a better way
handling this situation, a redirect or something?


exports.post_info_submit = function(req, res){

// do some updates here...

res.render('feedback', { title: 'Photo', msg : 'photo info
updaed.' })
};

Ryan Schmidt

unread,
Feb 8, 2012, 9:28:51 PM2/8/12
to expre...@googlegroups.com

If you're talking about the PRG pattern [1] then yes, in your POST handler for the given URL, you would handle the POST request (write the information to a database for example), then issue a redirect (to another URL, or to the same URL), and in the GET handler for that URL, you would render a template.

[1] http://en.wikipedia.org/wiki/Post/Redirect/Get


Angelo Chen

unread,
Feb 8, 2012, 9:44:49 PM2/8/12
to Express
hi Ryan,

redirect should be, i tried this:

res.redirect('/feedback', 302, { title: 'Photo', msg : 'photo info
updaed.', back_link : '/photoview/' + req.body.id })

the question is, how to send that json object in a redirect, it is not
available in the redirected page.

Thanks,


On Feb 9, 10:28 am, Ryan Schmidt <google-2...@ryandesign.com> wrote:
> On Feb 8, 2012, at 20:14, Angelo Chen wrote:
>
> > this is a route for a form submit, after processing, a feedback is
> > returned, problem is, the url is not changed,
> > stillhttp://localhost:8080/photo_update_submit, what is a better way

Dan MacTough

unread,
Feb 8, 2012, 10:03:55 PM2/8/12
to expre...@googlegroups.com
Angelo -  You'll need to either put that in req.session or pass it as query string parameters to the redirect url, i.e., res.redirect('/feedback?title=Photo&msg=photo%20info%20updated&etc=etc')

If you know the object is going to be small, you could stash it in a browser cookie, but yuck.

If there's another way, I'd like to know it, too.

tjholowaychuk

unread,
Feb 9, 2012, 11:37:39 AM2/9/12
to Express
I've never come across a reason to pass along a query-string with the
redirect. Typically:

- POST some /user data
- update the database
- redirect to /user/2 etc
- load in new stuff from the database
- display it

I think if you're using the QS for that you're probably going about it
the wrong way. As far as session-persisted messages go, just use
sessions :)

req.session.message = 'Photo info updated';

display / remove it when present

Angelo Chen

unread,
Feb 9, 2012, 8:58:46 PM2/9/12
to Express

this is a better way, thanks:

>  - POST some /user data
>  - update the database
>  - redirect to /user/2 etc
>  - load in new stuff from the database
>  - display it

Reply all
Reply to author
Forward
0 new messages