本文翻译自: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
});