chovy
chovy

Reputation: 75656

how to create a user with mongoose + express

I have a GET request route like this to /signup which shows a form:

#app.js
app.get('/signup', routes.signup);

#./routes/signup.js
exports.signup = function(req, res){
  res.render('signup', { title: 'Signup for a free account' });
};

#./views/signup.ejs
<form action="/signup" method="POST">...</form>

#app.js
app.post('/signup', ??);

How would I handle a POST to /signup to create a new user?

I don't get how to use routes.signup to handle a POST.

Upvotes: 0

Views: 207

Answers (1)

rdonadono
rdonadono

Reputation: 74

very quick and easy...

#app.js
app.get('/signup', routes.signup.get);

#./routes/signup.js
exports.signup = {};
exports.signup.get = function(req, res){
  res.render('signup', { title: 'Signup for a free account' });
};
exports.signup.post = function(req, res) {
// do your stuff
};

#./views/signup.ejs
<form action="/signup" method="POST">...</form>

#app.js
app.post('/signup', routes.signup.post);

Upvotes: 1

Related Questions