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

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

问题描述

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

  var http = require('http'); 
var fs = require('fs');
var path = require('path');
$ b http.createServer(函数(请求,响应){

console.log('请求开始');
console.log(request); $如果(filePath =='./')
filePath ='./index.html';

的console.log(文件路径);
变种extname = path.extname(文件路径);
变种的contentType = 'text / html的';
开关(extname){
'case'.js':
contentType ='text / javascript';
break;
case'.css':
contentType ='text / css';
break;
}

path.exists(filePath,function(exists){

if(exists){
fs.readFile(filePath,函数(错误,内容){
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将端口添加到env,所以你可以从那里拉它。切换您的听力:



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



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



你可以检查出的Node.js 这里 Heroku的文档。


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

Any idea ?

解决方案

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)

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

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

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

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