【问题标题】:Creating Routes in NodeJs在 NodeJs 中创建路由
【发布时间】: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">

知道了:

enter image description here

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');
  • 尝试=符号赋值

标签: node.js routes


【解决方案1】:

据我了解,您希望在注册表单中添加 action 属性。由于您已经创建了一个路由registeruser,它也有控制器user_sign_up,只需将registeruser 作为操作传递给您的表单。它应该可以工作。

<form method="POST" action="/registeruser">

</form>

编辑: 我创建了一个与你类似的结构,我的代码运行良好。尝试比较您的代码和我的代码,如果您的问题得到解决,请及时通知我。

【讨论】:

  • 我也做了同样的事情,但从概念上无法弄清楚它是如何找到函数的?
  • 这也行不通。当我安慰一些随机文本时。注意出现
  • 如果你想了解它如何找到函数的概念,很简单,快速路由器收集HTTP请求。当您设置 action="/registeruser" 时,表单数据作为 http 请求发送到路由器,然后控制器处理您的数据。如果可以正确理解,请接受答案。
  • 我肯定会的。但问题是它不起作用。它的说法'/ registeruser' 404 not found
  • 嘿,我认为它有效,我正在编辑我的答案,我会提供截图。
【解决方案2】:

我认为你必须使用= 符号来赋值

  Name = req.body.username;
  email = req.body.email;
  password = req.body.password;
  cpassword = req.body.cpassword;

【讨论】:

  • 在您的情况下,您将值从 req.body 分配给 Name 变量。 = 用于分配。
  • 但是在路由时我们必须获取用户 ID 或任何我们使用分号的东西。对吗?
  • 不,如果我们使用=,这意味着我们正在为某个变量赋值。如果我们使用:,意味着我们将该变量的引用提供给某个键{name: 'rupesh'},这里name 是键,rupesh 是值。
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2015-07-13
  • 2020-10-25
  • 2014-06-03
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多