Reputation: 439
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.
Upvotes: 2
Views: 8100
Reputation: 14953
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.
Upvotes: 7