【问题标题】:child_process in Node js Google App EngineNode js Google App Engine 中的 child_process
【发布时间】:2019-10-03 23:50:45
【问题描述】:

我有一个在 localhost 上运行的 Node Express 服务器。它使用 child_process 运行 C++ 独立可执行文件。

使用child_process的代码如下(应用程序创建output.txt):

app.post('/generate', async function(req, res)
{
    var input1 = req.body.input1;
    var input2 = req.body.input2;

    var execFile = require('child_process').execFile;

    var program = "path/to/executable";
    var args    = [input1, input2];

    var child = execFile(program, args,
        function (error, stdout, stderr){           
            console.log(error);
            console.log(stdout);
            console.log(stderr);

            const file = __dirname + "/output.txt"
            app.get('/output.txt', function (req, res) {
                res.sendFile(path.join(__dirname + '/output.txt'));
            });
            res.send("/output.txt");
        })
})

这在本地工作。

我现在正尝试使用 App Engine 将其部署在 Google Cloud Platform 上。

但是,当我访问我托管的网站并启动此 POST /generate 请求时,我没有得到预期的输出。在我的项目的 Google Cloud Platform 日志中,我可以看到以下错误:

 textPayload: "{ Error: spawn cpp/web_cpp_app/x64/Debug/web_cpp_app ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
" 

一开始我不明白这个错误,但现在我可以看到,如果我在本地运行同一个项目,但将独立可执行文件的路径设置为无效路径,我会得到同样的错误。我猜当我部署时,我的可执行文件不知何故不包括在内?

我需要在package.jsonapp.yaml 文件中添加一些特定的内容以包含可执行文件吗?

编辑: 会不会是应用引擎在Linux上运行,而我的可执行文件是针对Windows的?

【问题讨论】:

    标签: node.js express google-cloud-platform child-process


    【解决方案1】:

    ENOENT 表示“没有这样的文件或目录”,因此您的路径可能是错误的,或者容器无法将程序识别为可执行文件。

    但无论哪种方式,您都需要在部署时在项目目录中构建并包含您的child_process 程序的 linux 兼容二进制文件。您可以手动构建它,或者使用 Cloud Build 之类的东西在与 App Engine 相同的容器中编译它。

    【讨论】:

    • 仅供参考。我最终使用 Azure 而不是 Google Cloud Platform;它们允许创建 Windows 服务器,而不仅仅是 Linux。据我所知,GCP 只有 Linux webapp 服务器。
    • 所有主要的云提供商都支持 Windows 服务器,包括 GCP 和 AWS。
    • 好的,我无法从 GCP 中找到它。可能是因为我只看免费服务..?
    • 您可以在 GCE 和 GKE 上运行它们,但不能在 Appengine 上运行
    【解决方案2】:

    您对操作系统的看法是正确的,根据 Doc NodeJS 的 Appengine 标准使用 Ubuntu 操作系统,灵活使用 Debian

    关于我发现这个post的可执行兼容性

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 2017-12-14
      • 2020-06-16
      • 1970-01-01
      • 2017-11-05
      • 2019-08-21
      • 2021-01-24
      • 1970-01-01
      相关资源
      最近更新 更多