I'm a newbie to nodejs and currently working to run a certain cpp program running on a nodejs server with a click on a button.
After googling for a while, I've found that I can use something called 'Child process' to execute cpp program from a server side. And I've tried to include the following code in 'index.js' in '.nuxt' folder.
const { exec, spawn } = require("child_process");
exec('"C:\\Projects\\Monitoring_and_Diagnosis\\socketgraph\\build\\" spectrum_sim_cu', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
const child = spawn("./spectrum_sim_cu"); //where a is the exe file generated on compiling the code.
child.stdin.write("-10 10 (5/260) 70.03 0");
child.stdin.end();
child.stdout.on("data", (data) => {
console.log(`child stdout:\n${data}`);
});
});
I thought 'index.js' would be the right place to insert the above code since my cpp program needs to run and connect with the server as the nodejs server starts up. However, the whole '.nuxt' folder seems to be refreshed when I type 'npm run dev' to run the server.
Is it the right place or approach to run the cpp program on nodejs server. Can anyone help me out please? Thanks in advance.