在node.js中使用temp.mkdir时,在临时目录中找不到由被调用的exe生成的输出文件

问题描述

我尝试在不使用temp.mkdir的情况下运行EXE,并且它与输入和输出文件显示在主目录中的效果很好。但是,我希望能够使用临时目录,因为我希望节点创建输入文件,使用创建的输入文件运行程序,然后读取输出文件,然后删除临时目录以防止输入和输出文件在程序每次运行时被保存。

app.post('/Test',(req,res) => {
    const exec = require('child_process').exec;
    const execFile = require('child_process').execFile;
    const program = ".\\cpp\\a.exe";
    const input = req.body.input;

    temp.mkdir('test',(err,dirPath) => {
        const inputPath = path.join(dirPath,'input.txt');
        const outputPath = path.join(dirPath,'output.txt');
        fs.writeFile(inputPath,input,(err) => {
            if (err) throw err;
            const child = execFile(program,(error) => {
                if (error) throw error;
                fs.readFile(outputPath,function(err,data) {
                    if (err) throw err;
                    const output = data;
                    res.render("test",{
                        progOut: output
                    });
                    exec('del -r ' + dirPath,(error) => {
                        if (error) throw error;
                        console.log("Removed " + dirPath);
                    })
                });
            });
        });
    });
});

代码可能非常初级,效率低下,但这只是我要调试的某些部分。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)