NodeJS中有没有类似jQuery validate一样的中间件插件
1个回答
2017-12-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
nstallation
npm install express-validator
Usage
var util = require('util'),
express = require('express'),
expressValidator = require('express-validator'),
app = express.createServer();app.use(express.bodyParser());app.use(expressValidator([options]));app.post('/:urlparam', function(req, res) {
// checkBody only checks req.body; none of the other req parameters req.checkBody('postparam', 'Invalid postparam').notEmpty().isInt();
req.assert('getparam', 'Invalid getparam').isInt();
req.assert('urlparam', 'Invalid urlparam').isAlpha();
req.sanitize('postparam').toBoolean();
var errors = req.validationErrors();
if (errors) {
res.send('There have been validation errors: ' + util.inspect(errors), 400);
return;
}
res.json({
urlparam: req.param('urlparam'),
getparam: req.param('getparam'),
postparam: req.param('postparam')
});});app.listen(8888);
npm install express-validator
Usage
var util = require('util'),
express = require('express'),
expressValidator = require('express-validator'),
app = express.createServer();app.use(express.bodyParser());app.use(expressValidator([options]));app.post('/:urlparam', function(req, res) {
// checkBody only checks req.body; none of the other req parameters req.checkBody('postparam', 'Invalid postparam').notEmpty().isInt();
req.assert('getparam', 'Invalid getparam').isInt();
req.assert('urlparam', 'Invalid urlparam').isAlpha();
req.sanitize('postparam').toBoolean();
var errors = req.validationErrors();
if (errors) {
res.send('There have been validation errors: ' + util.inspect(errors), 400);
return;
}
res.json({
urlparam: req.param('urlparam'),
getparam: req.param('getparam'),
postparam: req.param('postparam')
});});app.listen(8888);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询