简体   繁体   English

(NodeJS)如何在“路由”中验证并发送到“控制器”[MVC]?

[英](NodeJS) How to Validation in 'Routes' and send to 'Controller' [MVC]?

i have a problem in my code: idols.js我的代码有问题: idols.js

router.post(
'/creating',

check('name', 'Min 1 and Max 50!').isLength({ min: 1, max: 50 }),

(req, res) => {
  
  const errors = validationResult(req);
  if (!errors.isEmpty()) {      
    alertError = errors.array();
    res.render('idols/create', {alertError});      
  }
  else {  
    //Next if value is good
  }      
});

and idolsController.js和idolsController.js

const idols = require('../models/idols'); 
class idolsController {

creating(req, res, next) {
    const idols_data = new idols(req.body);
    idols_data.save()
            .then(() => res.redirect('/idols/edit'))
            .catch(next());
}
module.exports = new idolsController;

my idols.js when not using logic validation不使用逻辑验证时的我的idols.js

router.post('/creating', idolsController.creating); 

--- My question: I want to convert the code in the 3rd paragraph to the code in the 1st paragraph (in this topic) thank for reading. --- 我的问题:我想将第 3 段中的代码转换为第 1 段中的代码(在本主题中),感谢阅读。

Your question is unclear.你的问题不清楚。 Please review and update请查看并更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM