Reputation:
I am trying to build a user registration forum for a web app.
To refactor my code I created a folder named api and defined some routes in index.js (/api/routes/index.js).
Now I wanted to route my signup form to this route in (/api/routes/index.js) so that the data can go to user_sign_up function which is defined in (api/controllers/users.js)
my app.js looks like:
// some code
var routesApi = require('./api/routes/index');
app.use('/api', routesApi);
// some code
my (/api/routes/index) looks like:
// some code
var ctrlevents = require('../controllers/users');
router.post('/registeruser', ctrlevents.user_sign_up);
// some code
module.exports = router;
In server folder of my app, I have views folder under which all .html files are present.
How do I define routes for action attribute in signup form?
my users.js looks like:
module.exports.user_sign_up = (req, res) => {
// some code
};
I tried:
<form method="POST" action="/registeruser">
got this:
Following is working but getting 500 status.
<form method="POST" action="/api/registeruser">
Adding user_sign_up function:
/* GET signup data */
module.exports.user_sign_up = (req, res) => {
Name: req.body.username;
email: req.body.email;
password: req.body.password;
cpassword: req.body.cpassword;
console.log('ghfhghgh');
console.log(email);
console.log(password);
console.log(cpassword);
req.checkBody('Name', 'Name is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('cpassword', 'Passwords do not match').equals(req.body.password);
let errors = req.validationErrors();
if (err) {
res.render('register', {
errors:errors
});
}
else {
let newUser = new User({
Name:Name,
email:email,
password:password
})
}
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) {
console.log(err);
}
newUser.password = hash;
newUser.save((err) => {
if(err) {
console.log(err);
return;
}
else {
req.flash('success', 'Welcome to TechPath');
req.redirect('/blog');
}
})
});
})
};
Upvotes: 0
Views: 650
Reputation: 890
I think you have to assign value using =
sign
Name = req.body.username;
email = req.body.email;
password = req.body.password;
cpassword = req.body.cpassword;
Upvotes: 0
Reputation: 4648
As I understand, you want the action
attribute in your sign up form. Since you already created a route registeruser
which also has controller user_sign_up
, just pass registeruser
to your form as action. It should be working.
<form method="POST" action="/registeruser">
</form>
Edit: I created a similar structure to yours, my code works well. Try to compare your code and my code and keep me updated if your issue is resolved or not.
Upvotes: 1