I'm stuck on what the best way to create a wrapper callback function in node/express that reuses the same parameters.
The thing is that the verification requires the same parameters the callback does. Is there a way to simplify this? I've looked for this on stack and google, but couldn't find an answer. I don't want to write req, res, next twice in the actual call. I understand the first req, res, next are being funneled into the callback, but it still feels weird to write it like this. It feels like there's definitely a better approach, but I just don't know what that is.
Here's the situation:
function verifyCaptcha(req, res, next, callback) {
let captchaResponse = req.body.captchaResponse;
let captchaSecret = "it's a secret";
let captchaURL = "https://www.google.com/recaptcha/api/siteverify?"
+ "secret=" + encodeURIComponent(captchaSecret) + "&"
+ "response=" + encodeURIComponent(captchaResponse) + "&"
+ "remoteip" + encodeURIComponent(req.connection.remoteAddress);
// request is ASYNC, that's why I need the callback
request(captchaURL, function(error, response, body) {
callback(req, res, next);
return;
});
};
router.post('/login', function(req, res, next) {
// example call INSIDE a route (here's the problem b/c params are repeated in the call
verifyCaptcha(req, res, next, function(req, res, next){
// do stuff
});
};