当已经使用了Web端口80时,如何在heroku上的任何端口上侦听? [英] how to listen on any port on heroku when a web port 80 is already in use?

查看:202
本文介绍了当已经使用了Web端口80时,如何在heroku上的任何端口上侦听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是heroku的新手,并且已经在heroku上部署了meteorjs应用程序.我的Meteorjs应用程序是一个Web应用程序,因此在构建完成后,它可以在heroku的端口80上运行.但是同时,我还想监听一个端口,例如:4000或任何端口,以便我可以捕获由任何第三方触发的webhook事件.想听.在我的本地计算机上,它与运行在端口上的webapp和在另一个端口上运行的侦听器完美运行,但是在我部署了运行的webapp且侦听器不侦听的heroku之后,它在heroku上运行.谁能帮助我或指导我.也欢迎任何新的解决方案.

I am new to heroku and i have deployed a meteorjs application on heroku. My Meteorjs application is a webapplication, so after the build is done it runs on heroku on port 80. But simultaneously i also want to listen on a port eg:4000 or any so that i can catch my webhook events fired by any third party i want to listen to. On my local machine it runs perfectly with webapp running on a port and listener running on another, but on heroku after i deploy its just the webapp which runs and listener doesn't listen. Can anybody help me or guide me. Any new solutions are also welcome..

在我的server/main.js中,我创建了一个facebook-bot-messenger机器人实例,下面是启动侦听器的代码

In my server/main.js i have made a bot instance of facebook-bot-messenger and below is the code to start the listener

bot.listen(4000);

在我的client/main.html中,我有一个html代码,它只是你好世界

In my client/main.html i have the html code which is just hello world

在本地计算机上,当我访问 http://localhost:3000 时,我可以看到helloworld,并且应用程序也在监听端口4000.

On local machine when i visit http://localhost:3000 i can see helloworld and app is also listening on port 4000.

在heroku上,当我访问 https://appname.herokuapp.com 时,我看到了helloworld.在这种情况下,端口为80,这很好,但是该漫游器没有监听,因为我在 https://上进行的任何帖子调用端口4000上的appname.heroku.com 没有响应.

On heroku when i visit https://appname.herokuapp.com i see helloworld . In this case the port is 80 which is fine, but the bot is not listening as any post calls i make on https://appname.heroku.com on port 4000 doesn't respond.

推荐答案

Heroku为您分配了端口,因此您无法设置自己的端口.您必须从PORT环境变量中读取端口号.在Node中,这将是process.env.PORT

Heroku assigns the port for you, so you can't set your own. You will have to read in the port number from the PORT environment variable. In Node this would be process.env.PORT

您也可以将||用于as和OR.例如,在以下情况下,它将在Heroku上从环境变量中读取端口号,但在本地它将默认为端口4000.

You can also use || for as and OR. For example in the below case, it will read in the port number from the environment variable while on Heroku, but locally it will default to port 4000.

const app = express();
app.set('port', (process.env.PORT || 4000));

//Start Server
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

这篇关于当已经使用了Web端口80时,如何在heroku上的任何端口上侦听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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