Heroku + node.js 错误(Web 进程未能在启动后 60 秒内绑定到 $PORT)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15693192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:03:08  来源:igfitidea点击:

Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

node.jsheroku

提问by user428900

I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn't let me write so much code, so I would just say that the running the code locally as well within my network shows no issue.

我有我的第一个 node.js 应用程序(在本地运行良好) - 但我无法通过 heroku 部署它(也是第一次使用 heroku)。代码如下。SO 不允许我写这么多代码,所以我只想说在本地运行代码以及在我的网络中运行没有问题。

 var http = require('http');
 var fs = require('fs');
 var path = require('path');

 http.createServer(function (request, response) {

    console.log('request starting for ');
    console.log(request);

    var filePath = '.' + request.url;
    if (filePath == './')
        filePath = './index.html';

    console.log(filePath);
    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.css':
            contentType = 'text/css';
            break;
    }

    path.exists(filePath, function(exists) {

        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                    response.writeHead(500);
                    response.end();
                }
                else {
                    response.writeHead(200, { 'Content-Type': contentType });
                    response.end(content, 'utf-8');
                }
            });
        }
        else {
            response.writeHead(404);
            response.end();
        }
    });

 }).listen(5000);

 console.log('Server running at http://127.0.0.1:5000/');

Any idea ?

任何的想法 ?

回答by redhotvengeance

Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env, so you can pull it from there. Switch your listen to this:

Heroku 会动态地为您的应用分配一个端口,因此您无法将端口设置为固定数字。Heroku 将端口添加到环境中,因此您可以从那里提取它。切换你的聆听:

.listen(process.env.PORT || 5000)

That way it'll still listen to port 5000 when you test locally, but it will also work on Heroku.

这样,当您在本地测试时,它仍会侦听端口 5000,但它也可以在 Heroku 上运行。

You can check out the Heroku docs on Node.js here.

您可以在此处查看 Node.js 上的 Heroku 文档。

回答by Royce Lee

The error happens when Heroku failed to bind the port or hostnameat server.listen(port, [host], [backlog], [callback]).

当 Heroku无法绑定端口或主机名时会发生错误server.listen(port, [host], [backlog], [callback])

What Heroku requires is .listen(process.env.PORT)or .listen(process.env.PORT, '0.0.0.0')

Heroku 需要的是.listen(process.env.PORT).listen(process.env.PORT, '0.0.0.0')

So more generically, to support other environments, use this:

所以更一般地,要支持其他环境,请使用:

var server_port = process.env.YOUR_PORT || process.env.PORT || 80;
var server_host = process.env.YOUR_HOST || '0.0.0.0';
server.listen(server_port, server_host, function() {
    console.log('Listening on port %d', server_port);
});

回答by zthomas.nc

It's worth mentioning that if your code doesn't specify a port, then it shouldn't be a webprocessand probably should be a workerprocess instead.

值得一提的是,如果你的代码不指定端口,那么它不应该是一个web过程,可能应该是一个worker过程,而不是。

So, change your Procfileto read (with your specific command filled in):

因此,将您更改Procfile为阅读(填写您的特定命令):

worker: YOUR_COMMAND

worker: YOUR_COMMAND

and then also run on CLI:

然后也在 CLI 上运行:

$ heroku scale worker=1

$ heroku scale worker=1

回答by vinesh

I had same issue while using yeoman's angular-fullstack generated project and removing the IP parameter worked for me.

我在使用 yeoman 的 angular-fullstack 生成的项目并删除对我有用的 IP 参数时遇到了同样的问题。

I replaced this code

我替换了这段代码

server.listen(config.port, config.ip, function () {
  console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});

with

server.listen(config.port, function () {
  console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});

回答by gregb

For those that are passing both a port and a host, keep in mind that Heroku will not bind tolocalhost.

对于那些同时传递端口和主机的人,请记住 Heroku不会绑定到localhost.

You must pass0.0.0.0for host.

你必须通过0.0.0.0主机。

Even if you're using the correct port. We had to make this adjustment:

即使您使用的是正确的端口。我们不得不做这样的调整:

# port (as described above) and host are both wrong
const host = 'localhost';
const port = 3000;

# use alternate localhost and the port Heroku assigns to $PORT
const host = '0.0.0.0';
const port = process.env.PORT || 3000;

Then you can start the server, as usual:

然后你可以像往常一样启动服务器:

app.listen(port, host, function() {
  console.log("Server started.......");
});

You can see more details here: https://help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

您可以在此处查看更多详细信息:https: //help.heroku.com/P1AVPANS/why-is-my-node-js-app-crashing-with-an-r10-error

回答by nexuzzz

In my case, I was using example from https://hapijs.com/

就我而言,我使用的是https://hapijs.com/ 中的示例

To fix the problem I replaced

为了解决我更换的问题

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

with

server.connection({
    port: process.env.PORT || 3000 
});

回答by Edgar Ortega

In my case, I was using Babel with the babel-plugin-transform-inline-environment-variablesplugin. Apparently, Heroku does not set the PORT env variable when doing a deployment, so process.env.PORTwill be replaced by undefined, and your code will fallback to the development port which Heroku does not know anything about.

就我而言,我将 Babel 与babel-plugin-transform-inline-environment-variables插件一起使用。显然,Heroku 在进行部署时没有设置 PORT 环境变量,因此process.env.PORT将被替换为undefined,并且您的代码将回退到 Heroku 一无所知的开发端口。

回答by Akshata Dabade

I had the same problem and changing my listening port from 3000 to (process.env.PORT || 5000) solved the problem!!

我遇到了同样的问题,将我的侦听端口从 3000 更改为 (process.env.PORT || 5000) 解决了问题!!

回答by Naveen Kumar V

The below steps resolved my solution:

以下步骤解决了我的解决方案:

Editing package.jsonas:

package.json编辑为:

...
"engines": {
"node": "5.0.0",
"npm": "4.6.1"
},
...

and Server.jsas:

Server.js为:

...
var port = process.env.PORT || 3000;
app.listen(port, "0.0.0.0", function() {
console.log("Listening on Port 3000");
});
...

回答by AmirRezaM75

I had the same issue because I didn't define Procfile. Commit a text file to your app's root directory that is named Procfile without a file extension. This file tells Heroku which command(s) to run to start your app.
web: node app.js

我有同样的问题,因为我没有定义 Procfile。将一个名为 Procfile 的文本文件提交到您的应用程序的根目录,没有文件扩展名。这个文件告诉 Heroku 运行哪些命令来启动你的应用程序。
web: node app.js