So, I'm trying to return the stdout from a command that I want to execute using exec
. However, since exec
is async I can't seem to return the stdout before the response sends an undefined
output. Since I am working withing a router.post
function (if that's even the name), the callback and async/await don't seem to help me.
Here is the code I use for the execution.
router.post("/select", (req, res, next) => {
const imageLocation = "backend/images/" + req.body.imagePath;
const execString = 'python backend/volatility/vol.py -f ' + imageLocation + ' imageinfo | grep "Suggested Profile(s) :"'
let output;
exec(execString, (err, stdout, stderr) => {
if (err) {
console.log(err);
}
output = stdout;
console.log(stdout);
});
console.log(output);
res.status(200).json({
message: "test",
output: output
})
console.log(req.body.imagePath);
})
What I want to have happen is a response that sends the output
after it has been set by exec
I might be overlooking something obvious, but I'm just not seeing it. I've looked everywhere and can't seem to find an answer.
EDIT:
Sorry to the people who already responded so fast. I'm trying to get the response over to angular and I think I might be doing something wrong over there.
This is my log.service.ts with the relevant function:
setImage(imagePath: any) {
let response;
this.http.post("http://localhost:3000/api/log/select", imagePath).subscribe((responseData) => {
response = responseData;
return response;
});
}
And here is my component for use with the html template:
imageChosen(event: any) {
console.log({imagePath: event.value});
this.suggestedProfile = this.logService.setImage({imagePath: this.selectedImage});
console.log(this.suggestedProfile);
}
Maybe it has something to do with the subscribe()
part?
I'm sorry if I caused confusion!