問題描述
這是我的第一個(gè) SE 問題.通常我可以通過這個(gè)很棒的網(wǎng)站很容易地找到任何問題的答案,但不幸的是,在這種情況下,無論是在這里還是在其他地方,我都找不到任何我正在尋找的東西.讓我解釋一下問題:
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:
我編寫了一個(gè) C++ 程序來進(jìn)行一些數(shù)值計(jì)算.它接受命令行參數(shù)并寫入標(biāo)準(zhǔn)輸出,并且在我運(yùn)行 OSX 的系統(tǒng)上運(yùn)行良好.
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 代碼來從表單中獲取輸入并將其作為命令行參數(shù)提供給可執(zhí)行文件.然后我按以下方式執(zhí)行名為factoriser"的二進(jìn)制文件:
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
}
}
以上在我的本地機(jī)器上完美運(yùn)行,但是當(dāng)我將它部署到 Heroku 然后嘗試輸入時(shí)(這里我嘗試了 2131),我得到一個(gè)錯(cuò)誤:
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 中的回調(diào)函數(shù).
that is given to the callback in exec.
所以我真的不知道該怎么辦,問題是 Heroku 沒有正確運(yùn)行可執(zhí)行文件.我對(duì) Heroku 的工作原理不是特別了解,我已經(jīng)閱讀了有關(guān) buildpack 等的信息,但僅執(zhí)行二進(jìn)制文件似乎是一個(gè)非常復(fù)雜的過程.是不是因?yàn)槲抑挥幸粋€(gè)dyno,不能運(yùn)行子進(jìn)程?
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?
如果有人能在這里為我指明正確的方向,我將不勝感激,看來我已經(jīng)完成了所有的努力,但無法克服最后的障礙.
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.
推薦答案
好的,我已經(jīng)開始工作了,很多人可能對(duì)此感興趣,所以我會(huì)發(fā)布我是如何做到的.
Ok, I have got it to work, this may be of interest to many so I will post how I did it.
問題是 Heroku 的架構(gòu)與我機(jī)器上的架構(gòu)不同,因此編譯后的程序根本無法在 Heroku 上運(yùn)行.為了解決這個(gè)問題,我創(chuàng)建了一個(gè) 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
它實(shí)質(zhì)上設(shè)置了一個(gè)可以訪問您的 Heroku 實(shí)例的 bash shell.
which essentially sets up a bash shell with access to your Heroku instance.
從這里,使用
$ make
然后scp
這個(gè)可執(zhí)行文件回到你的本地機(jī)器然后
Then scp
this executable back to your local machine and then
$ git add .
$ git commit -m "added working executable"
和
$ git push heroku master
然后運(yùn)行的可執(zhí)行文件將在 Heroku 應(yīng)用程序中,并且將像在本地主機(jī)上一樣運(yùn)行.
Then the working executable will be there on the Heroku app and will run just like on local host.
這篇關(guān)于如何從節(jié)點(diǎn)在 Heroku 上運(yùn)行可執(zhí)行文件,在本地工作的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!