Skip to content

Instantly share code, notes, and snippets.

@mborodov
Created February 11, 2015 16:57
Show Gist options
  • Save mborodov/cb4d2e44a8accfea6ed7 to your computer and use it in GitHub Desktop.
Save mborodov/cb4d2e44a8accfea6ed7 to your computer and use it in GitHub Desktop.
Служебный метод проверки обязательных параметров
//Метод парсит запрос и проверяет входные параметры
bodyCheck: function (params) {
//Входные параметры приходят через пробел
params = params.split(' ');
return function (req, res, next) {
// По умолчанию нет ошибки
var error = false,
body;
// Определяем body в зависимости от типа запроса
if (req.method == 'GET') {
body = req.query;
}
if (req.method == 'POST') {
body = req.body;
}
if (req.method == 'DELETE') {
body = req.params;
}
// Ищем наши параметры по имени в body, если какой-то отсутствует вернем ошибку
params.forEach(function (param) {
if(body[param] == undefined) {
error = true
}
});
// Если появилась ошибка вырнем её в виде респонса
return error ? res.send({"error": {"code": 400, "message": "Bad Request"}}) : next();
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment