0

I am making user login web application with express and mongodb. My project structure is like this,

project
   |-app.js
   |-models (contain mongoose model)
   |-controllers
   |      | - user.controller.js
   |- views
   |    | - layout.jade
   |    | - index.jade
   |    | - signup.jade

My app.js is simply for listening server, dependencies and routing. The simple code in app.js is,

//app.js
app.get('/', function(req,res){
  res.render('index');
});
app.post('/signup', function(req,res){
  res.render('signup');
});
app.post('/', controller.register);

And this is my user.controller.js file,

//user.controller.js
exports.register = function(req,res){
  var newUser = new User();

  newUser.username = req.body.username;
  newUser.email = req.body.email;
  newUser.password = req.body.password;

  newUser.save(function(err, user){
    if(err){
      console.log(err.message);
      console.log(err.errors.username.message);
      res.send('error registering user');
    } else{
      console.log(user);
      res.redirect('/signup');
    }
  });
};

signup.jade and index.jade are child of layout.jade file. layout.jade has nav bar and other simple stuffs. Index.jade has actual form that user can type their userID, password, and password confirmation. What I am tryying to do is, after user click the button in index.jade, user will go to redirect signup page and see you successfully signup!.

index.jade and signup.jade starts,

extend layout

block content
  div
    ...

I think my code will work, if they are separate jade files as someone explain in here Express, Jade, & NodeJS: Navigate between pages . But I didn't have any luck with this way. Can anyone help with it? Thanks!

1 Answer 1

0

I think I kinda figured out this one

//user.controller.js
exports.register = function(req,res){
  var newUser = new User();

  newUser.username = req.body.username;
  newUser.email = req.body.email;
  newUser.password = req.body.password;

  newUser.save(function(err, user){
    if(err){
      console.log(err.message);
      console.log(err.errors.username.message);
      res.send('error registering user');
    } else{
      console.log(user);
      res.redirect('/signup');
    }
  });

Inside of this file, I chosen res.redirect('/signup');. I changed this one to res.render('signup')' };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.