如何在Heroku中运行两台服务器 [英] How to run two servers in heroku

查看:93
本文介绍了如何在Heroku中运行两台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在angular 9中为前端和nodejs作为后端服务器构建了一个项目. 现在,我正在运行node appserver.js来运行后端服务器.默认情况下,它侦听端口9090.在另一个终端中,我正在运行ng serve,以正常在端口4200中运行我的角度应用程序. 我在angular应用程序中有一项服务,可以从服务器发送和接收消息.我已使用 Websocket类连接到端口9090.我的应用程序在本地运行良好.但是现在我该如何部署在heroku中. Heroku提供了一个随机端口号,该端口号将指定为process.env.PORT,我如何在我的websocket服务中获取该端口.我的问题是..

I have built a project in angular 9 for front end and nodejs as backend server. Now i am running node appserver.js for running the backend server. This listens to port 9090 as default. In another terminal i am running ng serve to run my angular app in port 4200 asusual. I have a service in angular app that sends and receives messages from server. I have used Websocket class to connect to the port 9090. My app works fine in local. But now how could i deploy in heroku. Heroku gives a random port number which will be specified as process.env.PORT how could i get that port in my websocket service. My questions are..

  1. 如何在heroku中部署两台服务器
  2. 如何将appserver.js中的端口号指定为angular的websocket服务中的端口号
  3. 是否可以使用.env文件和ProcFile来解决我的问题.
  4. heroku是否可以使用multi-buildpack来解决我的问题

请帮助

推荐答案

  1. 当每个服务器都需要一个端口时,您将无法部署两个单独的服务器.您将不得不将它们放入单独的应用程序中.在某些情况下,您可以组合使用Web服务器.定期部署服务器.

  1. You cannot deploy two separate servers when they each require a port. You will have to put them into separate apps. In some cases you can combine web servers. Deploying a server is done as regular.

在Heroku上部署Web服务时,Heroku为您提供了一个必须绑定到的端口.然后,您可以在<appname>.herokuapp.com下访问您的Web服务. (<-这就是为什么1.)要求您将它们放入单独的应用程序中.).此外,当您连接到Web服务时,您只需提供URL.该URL会自动翻译为<ipaddress>:<port>.因此,在您的前端中,您将不会指定端口号.您正在前端中指定websocket URL,而没有任何端口.
在您的Web服务器中,您绑定到process.env.PORT.

When deploying a web service on Heroku Heroku provides you a port to which you have to bind to. You can then visit your web service under <appname>.herokuapp.com. (<-- this is why 1.) requires you to put them into separate apps.). Furthermore when you connect to the webservice you merely give the URL. That URL is automatically translated into <ipaddress>:<port>. So in your frontend you are not going to specify a port number. You are specifying the websocket URL in your frontend without any port.
In your web server you bind to process.env.PORT.

.env文件不应被版本控制/提交.没用如果您需要环境变量,则可以通过Heroku的仪表板进行设置.不需要Procfile,因为您使用的是Node.js,它将查看位于package.json中的npm start脚本.但这并没有什么害处,因为它使内容清晰.

.env file shouldn't be versioned/committed. No use. If you require environment variables you can set them through Heroku's dashboard. Procfile is not required since you are using Node.js it will look into your npm start script located in package.json. But it doesn't hurt to have since it gives clarity.

对此没有多重构建包.


如果您的2台服务器严格区分并且使用单独的协议.一台使用http,另一台则可以将两台服务器捆绑在一起.这是一个示例:


If your 2 servers are strictly distinct and use separate protocols. One using http, the other ws you can bundle your two servers into one. Here is an example:

const http = require('http');
const path = require('path');
const express = require('express');
const WSServer = require('ws').Server;
const DateFormat = require('dateformat');

let wss;
let server;
const app = express();
app.use(express.static(path.join(__dirname, './../build')));

server = new http.createServer(app);
wss = new WSServer({ server })

this.wss = wss;
wss.on('connection', function(socket) {
    console.log(DateFormat(new Date(), 'm/d h:MM:ss TT'),
        `client connected to server (${wss.clients.size} total)`);
    socket.on('message', function(data) {
        console.log(data)
    });
    socket.on('close', function(code, desc) {
        console.log(DateFormat(new Date(),
            "h:MM:ss TT"),'client disconnected, total:', wss.clients.length);
    });
});
wss.on('listening', () => console.log('Websocket listening on port', config.get('port')));
wss.on('error', err => console.log('Websocket server error:', err));

server.on('error', err => console.log('Server error:', err));
server.listen(process.env.PORT);

项目示例:
https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/script/websock.js

Example in a project:
https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/script/websock.js

在项目中,将websocket服务器的后端扩展为包括一个用于服务静态文件的Express服务器.请注意,此更改仅存在于heroku分支中.

In the project the backend with the websocket server was extended to include an express server serving the static files. Note that this change only exists in the heroku branch.

您将在此提交中找到使该项目heroku兼容的所有相关更改:
https://github.com/vegeta897/d-zone/commit/63730fd7f44d2716a31fcae55990d83 >

You will find all the relevant changes that made that project heroku compatible in this commit:
https://github.com/vegeta897/d-zone/commit/63730fd7f44d2716a31fcae55990d83c84d5ffea

这篇关于如何在Heroku中运行两台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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