简体   繁体   English

第二个节点服务器(或端口)不会在生产中启动(Elastic Beanstalk)

[英]A second node server (or port) won't start in production (Elastic Beanstalk)

I have a Node/Angular app I'm trying to deploy.我正在尝试部署一个 Node/Angular 应用程序。 It uses two node servers: One to essentially serve the app;它使用两个节点服务器:一个主要为应用程序提供服务; another to get data from an API, when a specific port is requested by the app, and store that data locally.另一个从 API 获取数据,当应用程序请求特定端口时,并将该数据存储在本地。

I've got it working perfectly on my own local machine.我让它在我自己的本地机器上完美运行。 However, when I deploy to production environments -- either Heroku or AWS Elastic Beanstalk -- I find that the second script either won't run or won't start properly.但是,当我部署到生产环境(Heroku 或 AWS Elastic Beanstalk)时,我发现第二个脚本无法运行或无法正常启动。 The end result is, it doesn't get the data I need.最终结果是,它没有得到我需要的数据。

Here are the two scripts;这是两个脚本; they're both set to run in package.json under "start": "node main.js & node node-server.js"它们都设置为在“start”下的 package.json 中运行:“node main.js & node node-server.js”

main.js (again, this one seems to be serving the app just fine): main.js (同样,这个似乎可以很好地为应用程序提供服务):

var express = require('express');
var app = express();
app.use(express.static(__dirname + '/app'));
app.listen(process.env.PORT || 3000);

node-server.js (the one that doesn't seem to work; no data is gathered or populated in the app): node-server.js (似乎不起作用的那个;应用程序中没有收集或填充数据):

var http = require('http');
var port2 = 1234
var fs = require('fs');

//We need a function which handles requests and send response
function handleRequest(req, res) {
    request.get({
        url: 'http://sample-url.json',
        qs: {
            url: 'http://sampletool/pb/newsletter/?content=true'
        }
    }, function (err, result) {
        res.end(result.body);

        fs.writeFile('app/data.json', result.body, function (err) {
            if (err) return console.log(err);
            console.log('API data > data.json');
        });

    });
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(port2, function () {
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://0.0.0.0:%d", port2);
});

Then, the main Angular app calls this port ( http://0.0.0.0:1234 ) when the page is loaded, to request new data.然后,主 Angular 应用程序在页面加载时调用此端口 ( http://0.0.0.0:1234 ) 以请求新数据。

Elastic Beanstalk is using nginx, something I'm not super familiar with and that I don't have running on my local. Elastic Beanstalk 正在使用 nginx,这是我不太熟悉的东西,而且我没有在本地运行。

Is there something big I'm missing in configuring multiple node.js servers to be running on different ports in a production environment?在将多个 node.js 服务器配置为在生产环境中的不同端口上运行时,我是否遗漏了什么? Thanks in advance for any help.在此先感谢您的帮助。

For security reasons, cloud service providers typically allow the usage of only one port (which is dynamically and randomly assigned to the PORT environment variable) for an application to use from a node server.出于安全原因,云服务提供商通常只允许使用一个端口(动态随机分配给PORT环境变量)供应用程序从节点服务器使用。 Read this section from Heroku documentation to understand more about this.从 Heroku 文档中阅读此部分以了解有关此内容的更多信息。

This is why the main app ( main.js ) that uses process.env.PORT is working and the other app ( node-server.js ) that uses hard-coded 1234 is not.这就是为什么使用process.env.PORT的主应用程序 ( main.js ) 可以正常工作,而使用硬编码1234的其他应用程序 ( node-server.js ) 不能。

This question has some pointers about the feasibility of multiple ports on Heroku (though, there is no good news there, I am afraid). 这个问题有一些关于 Heroku 上多个端口的可行性的提示(虽然,我担心那里没有好消息)。

As how to go about fixing this, one thing that could be tried is to split this into two separate apps that are deployed separately with separate package.json etc.至于如何解决这个问题,可以尝试的一件事是将其拆分为两个单独的应用程序,这些应用程序分别使用单独的package.json等进行部署。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM