Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

c++ - How to run an executable on Heroku from node, works locally

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...