简体   繁体   English

Heroku + node.js:我有一个使用多个端口的服务器。 我怎样才能让 Heroku 分配它们?

[英]Heroku + node.js: I have a server which uses multiple ports. How can I get Heroku to allocate them?

Umm I'll try to be more clear..嗯,我会尽量说得更清楚。

In an application server I have written in node.js, I have inner-proxy for multiple ports:在我用 node.js 编写的应用服务器中,我有多个端口的内部代理:

  • in my 8080 port I have my rest api .在我的8080端口中,我有我的rest api
  • in my 3000 port I have my push server and chat .在我的3000端口中,我有我的推送服务器和聊天

I use the npm package subdomain-router for inner-routing to the port, exposing sub-domains in the 'front-end' which proxy back to those ports.我使用 npm 包subdomain-router进行端口的内部路由,在“前端”中公开子域,这些子域代理回这些端口。
code demonstration: ( <some-app> is not the real name of the app obviously)代码演示: <some-app>显然不是<some-app>的真名)

require('subdomain-router')
({
  host: '<some-app>.herokuapp.com',
  subdomains:
  {
    '': 8080,   // <some-app>.herokuapp.com <=> ::8080   --WORKS--
    'api': 8080,  // api.<some-app>.herokuapp.com <=> ::8080
    'chat': 3000, // chat.<some-app>.herokuapp.com <=> ::3000
    'push': 3000  // push.<some-app>.herokuapp.com <=> ::3000
  }
}).listen(process.env.PORT || 5000);

The API works great, though I cannot access it through <some-app>.herokuapp.com:8080 , but only through <some-app>.herokuapp.com and let the inner subdomain-router module do it's magic. API 工作得很好,虽然我不能通过<some-app>.herokuapp.com:8080访问它,但只能通过<some-app>.herokuapp.com并让内部subdomain-router模块来做它的魔法。
Also, I can't access the subdomains.另外,我无法访问子域。 When trying to access api.<some-app>.herokuapp.com I get No such app error page from heroku.当尝试访问api.<some-app>.herokuapp.com我从 heroku 得到No such app error page。

TL;DR accessing <some-app>.herokuapp.com works (redirects to /v1 path for my API), but unable to access <some-app>.herokuapp.com:8080 , <some-app>.herokuapp.com:3000 or chat.<some-app>.herokuapp.com . TL;DR访问<some-app>.herokuapp.com有效(重定向到我的 API 的/v1路径),但无法访问<some-app>.herokuapp.com:8080<some-app>.herokuapp.com:3000chat.<some-app>.herokuapp.com

When trying to access my API by specifying the port in the url ( like this: <some-app>.herokuapp.com:8080 ), I get the following error in my browser (google chrome): ERR_CONNECTION_REFUSED .当尝试通过在 url 中指定端口(例如: <some-app>.herokuapp.com:8080 )来访问我的 API 时,我在浏览器(谷歌浏览器)中收到以下错误: ERR_CONNECTION_REFUSED

My educated guess says that it might be something related to opening ports in heroku, but I have no clue on how to do it (tried googling ofc).我有根据的猜测说这可能与在 heroku 中打开端口有关,但我不知道如何去做(尝试使用谷歌搜索 ofc)。
It doesn't explain why I cannot access the sub-domains though.它并没有解释为什么我无法访问子域。

Would appreciate any light shed on this issue.希望对这个问题有所了解。
I'm new to heroku and it's getting really frustrating.我是 Heroku 的新手,它变得非常令人沮丧。

Thanks!谢谢!
Amit阿米特

Okay, after doing some research I've found out that opening ports in Heroku is disabled and not allowed .好的,经过一些研究,我发现在 Heroku 中打开端口是disablednot allowed

The only way around this is to use sub-domains and then in-app to use a proxy module (like subdomain-router which I use).解决这个问题的唯一方法是使用子域,然后在应用程序中使用代理模块(如我使用的subdomain-router )。

BUT - Heroku don't let you create sub-domains on their domain, meaning that your-app.herokuapp.com is fixed and cannot have sub-domains.但是- Heroku 不允许你在他们的域上创建子域,这意味着your-app.herokuapp.com是固定的,不能有子域。
In Heroku manuals, they demand you to have your own domain and dns provider to do such thing, by creating an A-alias (CNAME) in the dns table in your domain settings, that will refer to your app herokuapp domain, and then using the command heroku domains:add to add your domain to the allowed origin list.在 Heroku 手册中,他们要求您拥有自己的域和 dns 提供商来执行此操作,方法是在域设置的 dns 表中创建一个 A 别名(CNAME),该别名将引用您的应用程序 herokuapp 域,然后使用命令heroku domains:add将您的域heroku domains:add到允许的源列表中。

You can read more here .您可以在此处阅读更多内容。 It provides all the info you need.它提供了您需要的所有信息。

Hope it helped some.希望对一些人有所帮助。

I know this is an old post, but I wanted to provide an up-to-date response for reference and future use:我知道这是一篇旧帖子,但我想提供最新的回复以供参考和将来使用:

If you're using socket-io, binding to the same port is easy.如果您使用的是 socket-io,绑定到同一个端口很容易。 Other websocket libs should have a similar approach (from https://github.com/socketio/socket.io#how-to-use ):其他 websocket 库应该有类似的方法(来自https://github.com/socketio/socket.io#how-to-use ):

In conjunction with Express Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances.与 Express 结合 从 3.0 开始,express 应用程序已成为您传递给 http 或 http 服务器实例的请求处理函数。 You need to pass the Server to socket.io, and not the express application function.您需要将 Server 传递给 socket.io,而不是 express 应用程序函数。 Also make sure to call .listen on the server, not the app.还要确保在服务器上调用 .listen,而不是应用程序。

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);

You'll now have http & ws traffic flowing through a single port (Heroku doesn't route http/tcp separately, if it did your websockets wouldn't work period).您现在将拥有流经单个端口的 http 和 ws 流量(Heroku 不会单独路由 http/tcp,如果这样做,您的 websockets 将无法工作)。

I prefer this method due to environment parity & testing, ie no need to setup subdomains or port routing由于环境奇偶校验和测试,我更喜欢这种方法,即不需要设置子域或端口路由

I also learned about this today, I learned that if you run a service on a port in Heroku, you can still access it locally.我今天也了解了这一点,我了解到如果您在 Heroku 的端口上运行服务,您仍然可以在本地访问它。 Wouldn't work for above user's concern but it did fix my issue which led me to this question.对上述用户的关注不起作用,但它确实解决了我的问题,这导致我提出了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Heroku上的Node.js有2个端口 - Node.js on Heroku with 2 ports 在Heroku上托管时,我可以在非标准端口上监听node.js吗? - Can I have node.js listen on a non-standard port when hosted on Heroku? 我可以在node.js上直接在Heroku中使用imagemagick吗? - Can I directly use imagemagick in Heroku on node.js 如何使用MongoHQ和Node.js在Heroku上设置MongoDB? - How do I setup MongoDB on Heroku with MongoHQ and Node.js? 如何调试部署在 Heroku 上的 node.js 应用程序? - How could I debug a node.js App deploy on Heroku? 如何将数据从托管在 Heroku 上的 Node.js 应用程序发送到托管在完全独立的(Cpanel)服务器上的 PHP 文件? - How can I send data from a Node.js application hosted on Heroku to a PHP file hosted on a completely separate (Cpanel) server? 使用PM2,如何将node.js应用程序部署到同一服务器上的多个环境和端口? - Using PM2, how can I deploy my node.js app to multiple environments and ports on the same server? Heroku Node.js + Socket.io:部署到Heroku后,我得到一个ERR_CONNECTION_REFUSED - Heroku Node.js + Socket.io: After deploying to Heroku, I get a ERR_CONNECTION_REFUSED 如何从Heroku上的Node.js应用程序中的子文件夹重定向到静态javascript应用程序? - How can I redirect to static javascript app from subfolder in a Node.js app on Heroku? 如何从托管在不同环境中的文件执行由 Heroku 托管的 node.js 文件中的函数? - How can I execute functions in a node.js file hosted by Heroku from a file hosted in a different environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM