检查所有参数node.js的助手-Sails-Express [英] Helper for check all params node.js - sails - express
问题描述
我想检查控制器中的参数.目前,我正在那样做:
I want to check my params in my controllers. For the moment, I'm doing like that:
create: function (req, res) {
if(req.params.label && req.params.password){
// do stuff
}
}
但是我想更快地做到这一点.我想知道是否还没有工具.
But I want to do this more quickly. I'm wondering if there is no tools which already exist.
我想做这样的事情:
create: function (req, res) {
checkParams({label: {empty: false}, password: {empty: false}}, function(err){
// Do stuff
});
}
您知道有什么可以帮助我的吗?
Do you know something which could help me?
谢谢.
推荐答案
好的,我会自己回答.
我发现此模块非常有用. https://github.com/chriso/node-validator
I found this module, really useful. https://github.com/chriso/node-validator
我自己写了一个(并在这里= D的帮助下)管理验证的小帮手:
And I wrote, by my own (and with a little help from here =D) a little helper for manage validations:
感谢您的帮助;)该文件中有两个示例,我正在使用sails.js.但是,它可以与任何东西一起使用,显然,您需要验证程序包.
Thanks for the help ;) Two samples are available in the file, I'm using sails.js. But it can be used with anything, you need the validator package, obviously.
npm install validator --save
别忘了在文件开始处导入验证器.
Don't forget to import validator at the start of file.
var validator = require('../services/validator');
这篇关于检查所有参数node.js的助手-Sails-Express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!