I'm trying to make a POST request from node after a POST request from the client and pass the backend response back to the client. How can I pass the post response from the backend back to the post request originally from the frontend? I can access the promise in the async scope but how can I... err... push it/fart it back out to the client end? Thankyou.
async function sendToken() {
return await axios
.post("http://localhost:3001/Recaptcha", {token: token})
.then((res) => {
console.log('client recaptcha sent');
return res;
})
.catch((err) => console.log(err, 'recaptcha not sent'));
}
app.post('/Recaptcha', (req, res) => {
let p = new Promise((resolve, reject) => { // just to see if it would work
console.log(req.body.token)
const token = req.body.token
const secret = process.env.secret
async function validateRecaptcha() {
return await axios
.post(`https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${token}`)
.then((res) => {
console.log('server recaptcha sent');
resolve(res);
return res;
})
.catch((err) => console.log(err, 'recaptcha not sent'));
}
validateRecaptcha().then((res) => {
console.log("recaptcha data:", res.data);
//if (res.data.success === true) { // *if I could do this everything would work*
// res.send("HELLO")
//}
p.then((result) => { // I can access a promise result, but how do I push it... up?
console.log("p:", result.data)
res.send(result.data) // res.send is not a function error
})
})
console.log(validateRecaptcha.res) // undefined, pending promise
if (res.data.success === true) { // *how do I access this data to send to front?*
res.send("HELLO")
}
})
});