Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload current page in Express.js?

I'm current in /id/1234 page, and click a POST /add button, after that I want to reload /id/1234 in Experss.js:res.redirect(**How to get /id/1234 dynamicly, since I will be in /id/1235, /id/1236 page do a same POST /add action?**)

Thanks.

like image 706
nfpyfzyf Avatar asked Apr 05 '13 03:04

nfpyfzyf


People also ask

How do you refresh a page in JavaScript?

In JavaScript, you refresh the page using document. location. reload() . You can add the true keyword to force the reloaded page to come from the server (instead of cache).

How do you refresh an EJS page in node JS?

set('view engine', 'ejs'); app. use(bodyParser. urlencoded({ extended: false })); var requests; app. get('/', function(req, res) { res.

How do you refresh Nodemon?

When Nodemon restarts the ExpressJS server on changes, Livereload recreates the server and sends to the browser a refresh command when connected liveReloadServer. refresh("/"); . app.

Can't set headers after they are sent?

The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written.


2 Answers

I'd just like to add that in the version 4.x of Express you can use

res.redirect('back'); 

to automatically redirect back to the page the request came from. This is the equivilant of

res.redirect(req.get('referer')); 

that is mentioned in Peter Lyons answer

See also: http://expressjs.com/api.html#res.redirect

like image 134
Sean Dawson Avatar answered Sep 22 '22 23:09

Sean Dawson


To give you the answer you probably want, the browser will send a header called [Referer][1] which will have the URL of the /id/1234 page, so do:

res.redirect(req.get('referer')); 

However, your URL path design is probably poor if you need to do this. Better options might be submitting the form via AJAX without changing the URL or including the 1234 id in the form body and using that value from the POST request to generate the correct URL for the corresponding redirect.

like image 25
Peter Lyons Avatar answered Sep 22 '22 23:09

Peter Lyons