1

Node.js に飛び込もうとしているだけで、プロジェクトの達成に役立つと思われる基本的な機能をテストしています。そのうちの 1 つは、テキスト ファイルのデータを処理する C++ でコーディングした小さな .exe ファイルを実行することです。child_process.execFile がそれを達成するための最良の方法である可能性があることがわかりました。そこで、サーバーを起動して実行可能ファイルを呼び出す小さなスクリプトを作成しました。最初のテストでは、「サードパーティ」の .exe へのパスを設定しましたが、問題なく動作しました (プログラムは期待どおりに開始されました)。ただし、自分の .exe をターゲットにすると、パスは正しいですが、何も起こりません (fs.access でカウンターチェックします)。.exe をダブルクリックするだけでファイルを手動で実行しても問題なく動作し、それに応じて txt ファイルが処理されます。だから今、私はsthを取得するのだろうか. 根本的に間違っています。

これが私のコードです:

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);
});

コンソール出力は「...C:/path/to/file.exe exists」です。execFile はエラーをスローしません。あなたの助けをありがとう、そして私の初心者の言葉について謝罪してください!

4

0 に答える 0