(I'm new to stack overflow). I am trying to use req.json()
to send json to a request in Express.js. However, I am calling req.json()
from an asynchronous function. Therefore, I am receiving the error Error: Can't set headers after they are sent to the client
. I read this post but it did not answer my question exactly - for I think that I am getting the error for a different reason than the asker of the question in that that post. So, my question is, is it possible to send data to a req
object from an asynchronous function? If so, how is that done? If not, why is it not possible?
Here is some relevant and verifyable code (requested by @whodini9):
app.post("/command", async (req, res, next) => {
connections[id].exec(line, { // This is the async function
out: (stdout) => {
res.json({
output: stdout
});
}
}).start();
});