Heroku + node.js 错误(Web 进程未能在启动后 60 秒内绑定到 $PORT) [英] Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)

查看:31
本文介绍了Heroku + node.js 错误(Web 进程未能在启动后 60 秒内绑定到 $PORT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

有什么想法吗?

推荐答案

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

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:

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

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

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

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

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

这篇关于Heroku + node.js 错误(Web 进程未能在启动后 60 秒内绑定到 $PORT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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