Each res.send(200, line when is reached according to the logic is working fine:
const implementation = async (req, res, next) => {
try {
if (req.rut) {
const data = await someAPI();
res.send(200, data); // WORKING
} else {
const data2 = await SomeAPI2();
if (data2) {
res.send(200, data2}); // WORKING
}
res.send(400, 'Error'); // ERROR
}
} catch (error) {
res.send(400, error);
}
};
but when the code reach the line that uses res.send(400 I'm getting this error:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:561:11)
Why? I don't see that other res.send() lines are reached.
Even though i don't like the way you wrote the code this could solve your issue:
FYI:
i have added another else statement after you call the SomeAPI2().