问题
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:
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.
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
}
}
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 ")")
that is given to the callback in exec.
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.
回答1:
Ok, I have got it to work, this may be of interest to many so I will post how I did it.
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
Then
$ heroku run bash
which essentially sets up a bash shell with access to your Heroku instance.
From here, compile the executable using
$ make
Then scp
this executable back to your local machine and then
$ git add .
$ git commit -m "added working executable"
and
$ git push heroku master
Then the working executable will be there on the Heroku app and will run just like on local host.
来源:https://stackoverflow.com/questions/39685489/how-to-run-an-executable-on-heroku-from-node-works-locally