0

我正在定义一个处理提交请求的发布路线。我在 user.js 路由中编写了以下代码:

var express = require('express');
var router = express.Router();
var multer = require('multer');
var upload = multer({dest:'./uploads'});

router.post('/register', upload.single('profileimage'), function(req, res, next) {
    var name = req.body.name;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;
    if(req.file){
        var profileImage = req.file.filename;
    }
    else{
        var profileImage = 'noimage.jpg';
    }

    //form validation

    req.checkBody('name', 'Name feild is reqiured').notEmpty();
    req.checkBody('email', 'Email feild is reqiured').notEmpty();
    req.checkBody('email', 'Email feild is reqiured').isEmail();
    req.checkBody('username', 'Name feild is reqiured').notEmpty();
    req.checkBody('password', 'PasswordUser feild is reqiured').notEmpty();
    req.checkBody('password2', 'Password donot match').equals(req.body.password);
    //check Errors
    var errors = req.validationErrors();
    if(errors){
        res.render('register', {
            errors: errors
        });
        console.log(errors);
    }
    else{
        Console.log('No Errors');
    }

});

我的 app.js 已经在其中定义了 express-validator,如下所示:

var expressValidator = require('express-validator');
var app = express();
app.use(expressValidator({
    errorFormatter: function(param, msg, value){
        var namespace = param.split('.')
        , root = namespace.shift()
        , formParam = root;

        while(namespace.length) {
            formParam += '[' + namespace.shift()
        }
        return{
            param: formParam,
            msg: msg,
            value: value
        };
    }
}));

每次单击提交按钮测试验证时,浏览器上都会显示错误TypeError: req.checkBody is not a function 。请让我知道代码中缺少什么,或需要任何修改。

已经通过下面提到的链接,但它对我不起作用。: TypeError:req.checkBody 不是函数

4

1 回答 1

0

我做了一个简单的应用程序,尝试根据这个重写你的代码,它工作正常

app.js 文件

var express = require('express');
var expressValidator = require('express-validator');
var app = express();
var router = express.Router();
var bodyParser = require("body-parser");
var cors = require('cors');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());

app.use(expressValidator({
    errorFormatter: function(param, msg, value){
        var namespace = param.split('.')
        , root = namespace.shift()
        , formParam = root;

        while(namespace.length) {
            formParam += '[' + namespace.shift()
        }
        return{
            param: formParam,
            msg: msg,
            value: value
        };
    }
}));
// I pass app to the users.js file, because express validator I've assigned 
// to this express app instance
require('./users')(app);

app.listen(3300);

你可以跳过 body-parser 和 cors。users.js 文件看起来像

module.exports = function (app) {
    app.post('/register', function(req, res, next) {
        req.checkBody('name', 'Name feild is reqiured').notEmpty();
        var errors = req.validationErrors();
        if(errors){

            res.status(400).json({"errors": errors});
        }
        else{
            Console.log('No Errors');
            res.status(200).json({"foo": "bar"});
        }
    });
};

我使用 app.post('/register'),因为必须实例化 express 路由器,如果您为新的 express 对象创建路由器,您将无法连接到 express 验证器,因为该 express 验证器已分配给您的其他 express 对象在 app.js 文件中创建。

于 2017-11-01T07:03:36.193 回答