express-validator

杨飞语
2023-12-01

express-validation是一个中间件,它验证请求的body, params, query, headers 和 cookies ,并且如果任何配置的验证规则失败,返回一个错误的响应;

官方例子:

var express = require('express')
  , validate = require('express-validation')
  , http = require('http')
  , bodyParser = require('body-parser')
  , cookieParser = require('cookie-parser')
  , app = express();

app.use(bodyParser.json())
app.use(cookieParser())

app.set('port', 3000);

app.post('/login', validate(validation.login), function(req, res){
    res.json(200);
});

// error handler, required as of 0.3.0 
app.use(function(err, req, res, next){
  res.status(400).json(err);
});

http.createServer(app);
 类似资料: