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

本文翻译自:Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

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). 我有我的第一个node.js应用程序(在本地运行良好)-但是我无法通过heroku部署它(也是第一次使用heroku)。 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. 因此,我不允许编写太多代码,所以我只能说在本地以及在网络中运行代码都没有问题。

 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 ? 任何想法 ?


#1楼

参考:https://stackoom.com/question/13qW0/Heroku-node-js错误-Web进程在启动后-秒内未能绑定到-PORT


#2楼

Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku为您的应用程序动态分配一个端口,因此您不能将端口设置为固定编号。 Heroku adds the port to the env, so you can pull it from there. Heroku将端口添加到环境中,因此您可以从那里拉出它。 Switch your listen to this: 切换至此:

.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文档。


#3楼

The error happens when Heroku failed to bind the port or hostname at 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);
});

#4楼

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'));
});

#5楼

In my case, I was using Babel with the babel-plugin-transform-inline-environment-variables plugin. 就我而言,我将Babel与babel-plugin-transform-inline-environment-variables插件一起使用。 Apparently, Heroku does not set the PORT env variable when doing a deployment, so process.env.PORT will be replaced by undefined , and your code will fallback to the development port which Heroku does not know anything about. 显然,Heroku在部署时未设置PORT env变量,因此process.env.PORT将被undefined替换,并且您的代码将回退到Heroku一无所知的开发端口。


#6楼

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 
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值