[英]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 编写的应用服务器中,我有多个端口的内部代理:
8080
port I have my rest api .8080
端口中,我有我的rest api 。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:3000
或chat.<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 中打开端口是disabled和not 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.