由于未定义端口,Heroku 应用程序不断崩溃
Heroku App Keeps Crashing Because Port is Undefined
这是我在 server.js 文件中的设置:
const express = require('express');
const app = express();
const host = '0.0.0.0';
const PORT = process.env.PORT || 5001;
app.listen(PORT, host, () => {
console.log(`RABC Server is listening on port ${PORT}!`)
});
app.use(express.static('public'))
这是我的 Procfile 中的内容:
web:nodemon server.js
这是我收到的错误:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
这是我登录端口时得到的:
端口的值为:未定义
在我的 package.json 脚本中,我有:
"scripts": {
"start": "node server.js",
"build": "sanity build"
}
我没有 .env 文件,我不确定我是否应该有一个,以及里面有什么。
当我 运行 'heroku local web' 并在本地托管时一切正常。
我一生中从未部署或托管过应用程序,这是我第一次。
更新 server.js
为,
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5001;
app.use(express.static('public'))
app.listen(PORT, () => {
console.log(`RABC Server is listening on port ${PORT}!`)
});
移动app.listen
到最后,
It's just a convention to use a logical and safe order of initialization where you configure the server first before starting it.
参考:why app.listen should be at the end after all the requests? also why is it necessary?
此外,将 Procfile 更新为
web:npm run start
这是我在 server.js 文件中的设置:
const express = require('express');
const app = express();
const host = '0.0.0.0';
const PORT = process.env.PORT || 5001;
app.listen(PORT, host, () => {
console.log(`RABC Server is listening on port ${PORT}!`)
});
app.use(express.static('public'))
这是我的 Procfile 中的内容:
web:nodemon server.js
这是我收到的错误:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
这是我登录端口时得到的:
端口的值为:未定义
在我的 package.json 脚本中,我有:
"scripts": {
"start": "node server.js",
"build": "sanity build"
}
我没有 .env 文件,我不确定我是否应该有一个,以及里面有什么。
当我 运行 'heroku local web' 并在本地托管时一切正常。
我一生中从未部署或托管过应用程序,这是我第一次。
更新 server.js
为,
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5001;
app.use(express.static('public'))
app.listen(PORT, () => {
console.log(`RABC Server is listening on port ${PORT}!`)
});
移动app.listen
到最后,
It's just a convention to use a logical and safe order of initialization where you configure the server first before starting it.
参考:why app.listen should be at the end after all the requests? also why is it necessary?
此外,将 Procfile 更新为
web:npm run start