首页 文章

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

提问于
浏览
333

我有我的第一个node.js应用程序(在本地运行正常) - 但我无法通过heroku(第一次w / heroku)部署它 . 代码如下 . 所以我不会写这么多代码,所以我只想说在我的网络中本地运行代码没有问题 .

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

任何的想法 ?

16 回答

  • 13

    在所有的解决方案中,我没有按预期工作,我默认研究heroku .env文件应该维护约定PORT,process.env.PORT,heroku默认会查找关键字PORT .

    取消任何重命名,例如APP_PORT =而不是在你的env文件中使用PORT = .

  • 0

    它's worth mentioning that if your code doesn' t指定一个端口,那么它不应该是 web 进程,而应该是 worker 进程 .

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

    worker: YOUR_COMMAND

    然后还在CLI上运行:

    $ heroku scale worker=1

  • 1

    当Heroku failed to bind the port or hostnameserver.listen(port, [host], [backlog], [callback]) 时发生错误 .

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

    更一般地说,为了支持其他环境,请使用:

    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);
    });
    
  • 0

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

  • 8

    在heroku bash过程中,使用yargs等选项解析器将$ PORT的值传递给您的节点应用程序 .

    这是一个如何做到这一点的例子 . 在脚本对象上,在package.json中,添加一个start方法“node server --port $ PORT” .

    在服务器文件中,使用yargs从start方法的port选项(--port $ PORT)获取值:

    const argv = require('yargs').argv;
    const app = require('express')();
    
    const port = argv.port || 8081;
    
    app.listen(argv.port, ()=>{
        console.log('Probably listening to heroku $PORT now ', argv.port); // unless $PORT is undefined, in which case you're listening to 8081.
    });
    

    现在,当您的应用程序启动时,它将绑定到$ PORT的动态设置值 .

  • 21

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

  • 8

    对于那些通过端口和主机的人,请记住Heroku will not bind to localhost .

    You must pass 0.0.0.0 对于主持人 .

    即使您使用的是正确的端口 . 我们不得不进行这种调整:

    # 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;
    

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

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

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

  • 756

    如果像我一样,您正在配置Heroku以在部署时从 package.json 文件运行脚本,请确保您没有在该脚本中对 PORT 的值进行硬编码!如果您这样做,则'll end up like me and spend an hour trying to figure out why you'收到此错误 .

  • 0

    我有同样的问题,但有快递和apollo服务器 . 来自here的解决方案:

    需要做的唯一特殊考虑是允许heroku选择部署服务器的端口 . 否则,可能存在错误,例如请求超时 . 要将apollo-server配置为在运行时使用Heroku定义的端口,可以使用PORT环境变量定义的端口调用setup文件中的listen函数:

    > server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => { 
    > console.log(`Server ready at ${url}`); });
    
  • 1

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

    解决我替换的问题

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

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

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

  • 0

    The below steps resolved my solution:

    编辑 package.json 为:

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

    Server.js

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

    Heroku动态地为您的应用分配一个端口,因此您无法将端口设置为固定数字 . Heroku将端口添加到env,因此您可以从那里拉出它 . 切换你的听:

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

    这样,当你在本地测试时它仍会听取端口5000,但它也适用于Heroku .

    您可以在Node.js here上查看Heroku文档 .

  • 1

    在使用yeoman的angular-fullstack生成的项目并删除为我工作的IP参数时,我遇到了同样的问题 .

    我替换了这段代码

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

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

    无法为端口设置固定数量,heroku使用 process.env.PORT 动态分配 . 但是你可以添加它们,比如 process.env.PORT || 5000 . Heroku将使用第一个,而您的localhost将使用第二个 .

    您甚至可以添加回调功能 . 看下面的代码

    app.listen(process.env.PORT || 5000, function() {
        console.log("Server started.......");
    });
    
  • 2

    我有同样的问题,我可以解决问题,用IP替换'localhost'是'0.0.0.0'

相关问题