【发布时间】:2019-06-01 08:25:12
【问题描述】:
我正在尝试为网络应用构建用户注册论坛。
为了重构我的代码,我创建了一个名为 api 的文件夹,并在 index.js (/api/routes/index.js) 中定义了一些路由。
现在我想将我的注册表单路由到 (/api/routes/index.js) 中的这条路由,以便数据可以转到 (api/controllers/ 中定义的 user_sign_up 函数) users.js)
我的 app.js 看起来像:
// some code
var routesApi = require('./api/routes/index');
app.use('/api', routesApi);
// some code
我的 (/api/routes/index) 看起来像:
// some code
var ctrlevents = require('../controllers/users');
router.post('/registeruser', ctrlevents.user_sign_up);
// some code
module.exports = router;
在我的应用程序的服务器文件夹中,我有所有 .html 文件都存在于其下的视图文件夹。
如何为注册表单中的操作属性定义路由?
我的 users.js 看起来像:
module.exports.user_sign_up = (req, res) => {
// some code
};
我试过了:
<form method="POST" action="/registeruser">
知道了:
Following 正在运行,但状态为 500。
<form method="POST" action="/api/registeruser">
添加 user_sign_up 功能:
/* 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');
}
})
});
})
};
【问题讨论】:
-
您是否收到或到达“欢迎来到 TechPath”?
-
没有。唯一出现在控制台上的是 console.log('ghfhghgh');
-
尝试
=符号赋值