child_process 在节点 js 中 Google App Engine
child_process in Node js Google App Engine
我有一个在本地主机上运行的 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.json
或 app.yaml
文件中添加一些特定的东西,以包含可执行文件?
编辑:
会不会是 Linux 上的应用程序引擎 运行,而我的可执行文件是 Windows?
您关于 OS 的看法是正确的,根据这个 Doc NodeJS 的 Appengine 标准使用 Ubuntu OS,灵活使用 Debian
关于可执行兼容性我发现了这个
ENOENT 表示 "no such file or directory",因此您的路径可能是错误的,或者容器无法将程序识别为可执行文件。
但无论哪种方式,您都需要在部署时在项目目录中构建并包含 linux 兼容的 child_process
程序二进制文件。您可以手动构建它,或使用 Cloud Build 之类的东西在与 App Engine 相同的容器中编译它。
我有一个在本地主机上运行的 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.json
或 app.yaml
文件中添加一些特定的东西,以包含可执行文件?
编辑: 会不会是 Linux 上的应用程序引擎 运行,而我的可执行文件是 Windows?
您关于 OS 的看法是正确的,根据这个 Doc NodeJS 的 Appengine 标准使用 Ubuntu OS,灵活使用 Debian
关于可执行兼容性我发现了这个
ENOENT 表示 "no such file or directory",因此您的路径可能是错误的,或者容器无法将程序识别为可执行文件。
但无论哪种方式,您都需要在部署时在项目目录中构建并包含 linux 兼容的 child_process
程序二进制文件。您可以手动构建它,或使用 Cloud Build 之类的东西在与 App Engine 相同的容器中编译它。