如何在节点上运行Heroku上的可执行文件,在本地工作 [英] How to run an executable on Heroku from node, works locally

查看:148
本文介绍了如何在节点上运行Heroku上的可执行文件,在本地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一个SE问题。通常我可以很容易地通过这个伟大的网站找到任何答案,但不幸的是,在这个场合我无法在这里或其他地方找到任何关于我所寻找的东西。让我解释一下问题:

我写了一个C ++程序来做一些数值计算。它需要命令行参数和写入标准输出,并在我的运行OSX的系统上正常工作。



我想在网上为我的同伴主持以更轻松地尝试它,并且所以我编写了一些Node.js和Express代码从窗体获取输入,并将其作为可执行文件的命令行参数。然后我通过以下方式执行称为'factoriser'的二进制文件:

  const exec = require('child_process')。exec; 
app.post('/',function(req,res){
var input = req.body.numberinput; //在网页上输入的数字

const child = exec('./ numericcomp'+ input,{timeout:20000},function(error,stdout,stderr){
// Code here write stdout to the page
}
}

上面的代码在我的本地机器上完美工作,但是当我将它部署到Heroku并尝试输入时尝试2131)我得到一个错误:

 错误:命令失败:./numericcomp 2131 ./numericcomp:3:./ numericcomp:语法错误:单词意外(期待))

给予exec中的回调。



所以我真的不知道该怎么做,问题在于Heroku并没有正确运行可执行文件,我对Heroku的工作原理并不十分了解。 ,我已经阅读了关于构建包的信息等,但它似乎是一个非常复杂的过程,只是执行一个二进制文件因为我只有一个测功机,它不能运行子进程?



如果有人能指出我在这里正确的方向,我将非常感激,似乎我已经完成了所有的艰苦工作,但无法克服最后的障碍。

解决方案

好的,我已经完成了它的工作,这可能会让很多人感兴趣,所以我会发布我是如何做到的。



问题在于Heroku的架构与我的机器不同,因此编译程序根本不会在Heroku上运行。为了解决这个问题,我创建了一个makefile来编译C ++源代码,并使用

  $ git push heroku master 

然后

  $ heroku run bash 

它基本上设置了一个可以访问你的Heroku实例的bash shell。



从这里开始,使用

  $ make $ b编译可执行文件$ b  

然后 scp 这个可执行文件返回到本地机器那么

  $ git add。 
$ git commit -m新增可执行文件

  $ git push heroku master 



<然后,正在运行的可执行文件将会在Heroku应用程序中运行,并将像在本地主机上一样运行。


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.

解决方案

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.

这篇关于如何在节点上运行Heroku上的可执行文件,在本地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆