如何从节点在 Heroku 上运行可执行文件,在本地工作
这是我的第一个 SE 问题.通常我可以通过这个很棒的网站很容易地找到任何问题的答案,但不幸的是,在这种情况下,无论是在这里还是在其他地方,我都找不到任何我正在寻找的东西.让我解释一下问题:
This is my first SE question. Usually I can find an answer to anything fairly easily through this great website, but unfortunately on this occasion I can't find anything on what I am looking for, either here or elsewhere. Let me explain the problem:
我编写了一个 C++ 程序来进行一些数值计算.它接受命令行参数并写入标准输出,并且在我运行 OSX 的系统上运行良好.
I have written a C++ program to do some numerical computations. It takes command line arguments and writes to stdout and works fine on my system running OSX.
我想在线托管它,让我的同行更轻松地试用它,因此我编写了一些 Node.js 和 Express 代码来从表单中获取输入并将其作为命令行参数提供给可执行文件.然后我按以下方式执行名为factoriser"的二进制文件:
I want to host this online for my peers to try it out more easily, and so I wrote some Node.js and Express code to take an input from a form and give that as a command line argument to the executable. I then execute the binary called 'factoriser' in the following way:
const exec = require('child_process').exec;
app.post('/', function (req, res) {
var input = req.body.numberinput; //Number entered on the webpage
const child = exec('./numericcomp ' + input, {timeout: 20000}, function(error, stdout, stderr) {
//Code here writes stdout to the page
}
}
以上在我的本地机器上完美运行,但是当我将它部署到 Heroku 然后尝试输入时(这里我尝试了 2131),我得到一个错误:
The above works perfectly on my local machine but when I deploy it to Heroku and then try an input (here I tried 2131) I get an error of:
Error: Command failed: ./numericcomp 2131 ./numericcomp: 3: ./numericcomp: Syntax error: word unexpected (expecting ")")
给 exec 中的回调函数.
that is given to the callback in exec.
所以我真的不知道该怎么办,问题是 Heroku 没有正确运行可执行文件.我对 Heroku 的工作原理不是特别了解,我已经阅读了有关 buildpack 等的信息,但仅执行二进制文件似乎是一个非常复杂的过程.是不是因为我只有一个dyno,不能运行子进程?
So I really don't know what to do, the issue is that Heroku just isn't running the executable properly. I am not particularly knowledgable about how Heroku works, I have read through info on buildpacks etc. but it seems a very complicated process just to execute a binary. Is it because I only have one dyno and it can't run the child process?
如果有人能在这里为我指明正确的方向,我将不胜感激,看来我已经完成了所有的努力,但无法克服最后的障碍.
I would be very grateful if someone could point me in the right direction here, it seems I have done all the hard work but can't get over the final hurdle.
推荐答案
好的,我已经开始工作了,很多人可能对此感兴趣,所以我会发布我是如何做到的.
Ok, I have got it to work, this may be of interest to many so I will post how I did it.
问题是 Heroku 的架构与我机器上的架构不同,因此编译后的程序根本无法在 Heroku 上运行.为了解决这个问题,我创建了一个 makefile 来编译 C++ 源代码并使用
The problem was that Heroku's architecture is not the same as that on my machine and hence the compiled program simply would not run on Heroku. To get around this I created a makefile to compile the C++ source code and pushed this to Heroku using
$ git push heroku master
然后
$ heroku run bash
它实质上设置了一个可以访问您的 Heroku 实例的 bash shell.
which essentially sets up a bash shell with access to your Heroku instance.
从这里,使用
$ make
然后scp
这个可执行文件回到你的本地机器然后
Then scp
this executable back to your local machine and then
$ git add .
$ git commit -m "added working executable"
和
$ git push heroku master
然后运行的可执行文件将在 Heroku 应用程序中,并且将像在本地主机上一样运行.
Then the working executable will be there on the Heroku app and will run just like on local host.
相关文章