我是node.js的新手,我正在使用express-validator
作为中间件来运行此文件,以验证我的请求中的POST数据。但是我收到了一个错误:
(function(){
var express = require("express");
var expressValidator = require('express-validator');
var app = express();
app.all("*", function(req, res, next) {
console.log("inside my middleware")
next();
});
app.configure(function(){
app.set("port", process.env.PORT || 3000);
app.use(express.json());
app.use(express.urlencoded());
app.use(expressValidator());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.errorHandler());
});
app.post("/test", function(req, res){
req.assert("email", "email is not valid").isEmail();
res.send(200);
})
app.listen(app.get("port"), function(){
console.log("API Server is available on port %s", app.get("port"));
});
}());
它告诉我 POST 上的/test
请求没有方法断言,它应该是express-validator
中间件的一部分。我做错了什么?
答案 0 :(得分:-1)
根据它的docs,您还必须使用明确的BodyParser中间件:
app.use(express.bodyParser());
app.use(expressValidator([options])); // this line must be immediately after express.bodyParser()!