Skip to content

Instantly share code, notes, and snippets.

@L1fescape
Last active December 22, 2015 20:29
Show Gist options
  • Save L1fescape/6527231 to your computer and use it in GitHub Desktop.
Save L1fescape/6527231 to your computer and use it in GitHub Desktop.
Required params for NodeJS. Basic working example, needs improvements.
function required(params) {
return function (req, res, cb) {
if (params.every(function (elem) { return req.body.hasOwnProperty(elem); }))
cb();
else
res.send(400, {status:400, message: 'Missing required body parameters.'});
};
};
/*
* Routes
*/
// GET Some Route
app.get("/route", required(["user_string"]), function(req, res) {
var respObj = {}
user_string = req.body.user_string;
res.writeHead(200, {'content-type': 'application/json'});
res.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment