Created
February 11, 2015 16:57
-
-
Save mborodov/cb4d2e44a8accfea6ed7 to your computer and use it in GitHub Desktop.
Служебный метод проверки обязательных параметров
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Метод парсит запрос и проверяет входные параметры | |
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