admin管理员组文章数量:1333201
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.
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.
Share Improve this question asked Mar 7, 2014 at 12:10 NeoaresNeoares 4391 gold badge11 silver badges24 bronze badges 2-
What is your database ? I'm supposing mongo with mongoose. Your should maybe add an unique key for your user (for email or pseudo...)
email: { type : String, index: { unique: true }},
– Cyrbil Commented Mar 7, 2014 at 12:17 - I have a unique key. Although you press F5, the user is not created cause it's Unique. But the problem it's I don't want to resend the data. – Neoares Commented Mar 7, 2014 at 12:20
1 Answer
Reset to default 7The 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.
本文标签: javascriptHow to avoid resending data on refresh in Nodejs with ExpressStack Overflow
版权声明:本文标题:javascript - How to avoid re-sending data on refresh in Node.js with Express - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742284725a2446727.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论