I am just trying to dive into Node.js and I am testing basic functionalities that I consider helpful to accomplish my project. One of them would be executing a small .exe file I coded in C++ that processes data from text files. I figured out that child_process.execFile might be the best method to achieve that. So I wrote a little script to start a server and invoke an executable. For the first test, I set the path to a "third-party"-.exe and it worked just fine (program is started as expected). However, when I target my own .exe nothing happens at all, although the path is correct (which I countercheck with fs.access). Manually executing the file by mere double-clicking on the .exe also works totally fine and txt-files are accordingly processed. So now I wonder if get sth. fundamentally wrong, e.g. does the .exe need to fulfil a certain condition to be executable with execFile?
Here is my code:
var http = require('http');
const fs = require('fs');
var server = http.createServer(function(req, res){
console.log('Request was made: ' + req.url);
res.writeHead(200, {'Content-Type': 'text/plain'});
});
server.listen(3000, '127.0.0.1');
console.log('Listening to port 3000');
var executablePath = "C:/path/to/file.exe";
fs.access(executablePath, fs.constants.F_OK, (err) => {
console.log(`${executablePath} ${err ? 'does not
exist':'exists'}`);
});
const execFile = require('child_process').execFile;
const child = execFile(executablePath, (error, stdout, stderr) => {
if (error) {
console.error('stderr', stderr);
throw error;
}
console.log('stdout', stdout);
});
The console output is "...C:/path/to/file.exe exists". execFile does not throw any error. Thx for your help and apologies for my noob language!