3

I am using Express v.2.4.6 (Node.js - v.0.6.2). I am trying to render (or redirect to) a new page once POST is called (as opposed to GET). I am able to render/redirect when GET is called. However, I cannot seem to render a page when POST is called in Express. I am not sure if this is even possible though the guide on the Express site does mention an example where you can redirect once POST is called. The relevant code is given below (client is sending the form in JSON). I can parse through the JSON message successfully in Node.j.

Sample route:

app.post('/signup', function(req, res){
res.redirect('index');
//res.render('index');
});

No exceptions are thrown, but the index page does not get rendered nor redirected. Any feedback will be appreciated.

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
ali haider
  • 19,175
  • 17
  • 80
  • 149

2 Answers2

2

Maybe you call it from $.ajax from client-side It works well if you call it from server-side

Please refer to this question. Express.js Won't Render in Post Action

Community
  • 1
  • 1
Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
  • this was on the server not client - I did not see any 'redirect' calls in your example unless I missed it. I actually had to rely on client side redirecting in the case mentioned above (unlike my comment which I added too early). – ali haider Oct 13 '14 at 02:04
  • Hmmm, I should add the code to my example. I was calling from client side, I just didn't copy the code. Hmmm, if it is possible, you might need to consider moving the functionality not relying to $.ajax if possible. If not, I'll tell you in the future if I got anything – Aminah Nuraini Oct 13 '14 at 11:13
  • Maybe you return url from server side and do 'location.href = url;' as suggested in this link http://stackoverflow.com/questions/9789418/nodejs-expressjs-jade-post-no-render/26329333#26329333 – Aminah Nuraini Oct 13 '14 at 11:19
1

res.redirect('/') is most likely what you wanted there, but you can render in any route, redirecting is just a convention that most people use

Jay Wick
  • 12,325
  • 10
  • 54
  • 78
  • Thank you for responding - much appreciated. Perhaps the way I asked the question was not correct. The issue is that once post is called, I cannot seem to render or redirect to any page. I have tried both res.render and res.redirect. The code after the redirect/render gets called & I do not see any exceptions on the server end - however, the new page does not get rendered in firefox/chrome. Any suggestions/insight into this matter will be appreciated. Once again, thanks for the response. – ali haider Dec 01 '11 at 06:00
  • Probably the POST route is fine but your `app.get('/index')...` isn't set up correctly. Have you tried looking at the response headers in Chrome inspector or via `curl -i`? You should see a 302 redirect to 'index', but you may want '/index' instead. Post a larger code snippet ad we can help you out. – Peter Lyons Dec 02 '11 at 04:28
  • Thank you (to both TJ & Peter) - much appreciated. I was able to render a new page with a new (post) route. I am still trying to figure out what caused the issue with the original route. – ali haider Dec 02 '11 at 08:08