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
Add a ment  | 

1 Answer 1

Reset to default 7

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.

本文标签: javascriptHow to avoid resending data on refresh in Nodejs with ExpressStack Overflow