By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,776 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,776 IT Pros & Developers. It's quick & easy.

Express.js redirect user after sign up

P: 97
I can't find an answer to this: What do I do after a new user signs up to my express.js app?

I have a view, and this in the routes/index file that correspnds:
Expand|Select|Wrap|Line Numbers
  1. router.get('/users/firstSignIn', function(req, res, next) {
  2.   res.render('uesrs/firstSignIn', { title: 'title' });
  3. });
  4.  

And this signup function in my users controller:
Expand|Select|Wrap|Line Numbers
  1. exports.create = function(req, res, password) {
  2.  
  3.   if(req.method.toLowerCase() != "post") {
  4.     res.render("signup.jade", {layout: false});
  5.   }
  6.   else {
  7.      new user(req.body).save();
  8.      res.redirect("/users/firstSignIn");
  9.      //res.send("ok");  // USE THIS TO REDIRECT AFTER SIGNUP
  10.   }
  11. }
  12.  

This is in my app.js:
Expand|Select|Wrap|Line Numbers
  1. app.get('/users/create', users.create);
  2. app.post('/users/create', users.create);
  3.  

When I sign up a new user, I can't figure out how to redirect them. Am I going about this all wrong, or I just need to add to my controller?
May 12 '16 #1

✓ answered by Dormilich

you use two different functions for the get and post route, not one function that does it all.

if you want to use export, then you need to export two functions, not one:

Expand|Select|Wrap|Line Numbers
  1. exports.create = {
  2.     get: function(req, res, next) {},
  3.     post: function(req, res, next) {}
  4. };
  5.  
Expand|Select|Wrap|Line Numbers
  1. app.get('/users/create', users.create.get);
  2. app.post('/users/create', users.create.post);
  3.  

Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you use two different functions for the get and post route, not one function that does it all.

if you want to use export, then you need to export two functions, not one:

Expand|Select|Wrap|Line Numbers
  1. exports.create = {
  2.     get: function(req, res, next) {},
  3.     post: function(req, res, next) {}
  4. };
  5.  
Expand|Select|Wrap|Line Numbers
  1. app.get('/users/create', users.create.get);
  2. app.post('/users/create', users.create.post);
  3.  
May 12 '16 #2

P: 97
I'm not sure if it's just my system (my work computer is pretty ordinary), but I changed it to this:

Expand|Select|Wrap|Line Numbers
  1. exports.create = {
  2.  
  3.     post: function(req, res, next) { // password
  4.  
  5.         if(req.method.toLowerCase() != "post") {
  6.         res.render("signup.jade", {layout: false});
  7.         }
  8.  
  9.         else {
  10.             new user(req.body).save();
  11.         }
  12.     },
  13.  
  14.     get: function(req, res, next){
  15.         res.redirect("/users/firstSignIn");
  16.     }
  17.  
  18. }
  19.  
And it's doing some strange things. It's posting the new user to the Db, but it's adding the user twice. It's also not loading the new page (/users/firstSignIn). The browser crashes with an error message saying: "the localhost page isn't working. localhost didn't send any data"

But when I check the db the data is there, as I said twice.
May 13 '16 #3

Dormilich
Expert Mod 5K+
P: 8,639
first, if(req.method.toLowerCase() != "post") is pointless as the request method of a POST route is always POST. second, you never send a response after inserting data into the db.
May 13 '16 #4

P: 97
That was correct. The function now looks like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. exports.create = {
  3.   post: function(req, res, next) {
  4.     new user(req.body).save();
  5.     res.redirect("/users/firstSignIn");
  6.   },
  7.   get: function(req, res, next){
  8.     res.redirect("/users/firstSignIn");
  9.   }
  10. }
  11.  
  12.  
Now I just need to customise it.
May 18 '16 #5

Post your reply

Sign in to post your reply or Sign up for a free account.