Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  164] [ 1]  / answers: 1 / hits: 6772  / 10 Years ago, fri, march 7, 2014, 12:00:00

First of all, I want to apologyze for my english.



I have a Node.js aplication using Express and Jade. My main page has a form, wich allows people to register. I send the data via POST. I have a handler like this:



app.post('/userCreate', user.create);


And:



exports.create = function(req, res){
//creates the user in the database
res.render('./game/main'); //goes to the main game page
}


My problem is that when people registers, and I render the main page, in the URL appears localhost:3000/userCreate. So, if I press F5, all data is sended again via POST, because it's catching '/userCreate' again, and it tries to create another User.



I think an option could be change the URL after creating a user, but I realized that neither 'render' nor 'redirect' methods changes it.



I've been reading about post-redirect-get pattern, but I don't know how to do a get without a form, I mean, via javascript instead of HTML.


More From » node.js

 Answers
3

The problem is here:



res.render('./game/main');  //goes to the main game page


You don't actually go to the main game page, but rather render that view for the current url. Instead you should redirect the browser to another route:



res.redirect('/game'); // Or whatever url you have


and create a separate route for that url:



app.get('/game', game.index);


and let that route render the game:



exports.index = function (req, res) {
res.render('./game/main');
};


Now f5 will reload the game page instead of the form POST. Even if you want to use the same url as the form is on you should redirect to the GET-route of the same url, to prevent reposts.


[#47084] Thursday, March 6, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;