html - Heroku Node.js - 错误 R10(引导超时)-> Web 进程无法绑定到 $PORT
问题描述
当我在 Heroku 上部署我的网站时,它说:-
应用程序错误
应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请查看您的日志以获取详细信息。您可以从 Heroku CLI 使用命令 Heroku logs --tail 执行此操作
在日志中,当我检查时,似乎没有什么大不了的。它给出了: -
2020-09-20T23:56:47.790226+00:00 app[web.1]:
2020-09-20T23:56:48.040459+00:00 app[web.1]: SERVER IS RUNNIG AT PORT 8000
2020-09-20T23:56:48.040768+00:00 app[web.1]: Server runnung at http://127.0.0.1:5500
2020-09-20T23:57:44.991177+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-09-20T23:57:45.017244+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-09-20T23:57:45.087257+00:00 heroku[web.1]: Process exited with status 137
2020-09-20T23:57:45.132990+00:00 heroku[web.1]: State changed from starting to crashed
2020-09-21T02:05:44.125661+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sheetsw.herokuapp.com request_id=b87a2aae-a1d1-44d4-9f4e-2d10421edec6 fwd="27.60.151.127" dyno= connect= service= status=503 bytes= protocol=https
2020-09-21T02:05:46.906817+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=sheetsw.herokuapp.com request_id=a46e447c-e111-4954-a6c6-a1e928013a82 fwd="27.60.151.127" dyno= connect= service= status=503 bytes= protocol=https
2020-09-21T02:06:05.637299+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=sheetsw.herokuapp.com request_id=d6aa7d9b-60eb-450c-8ceb-c063faad3917 fwd="27.60.151.127" dyno= connect= service= status=503 bytes= protocol=https
你能告诉我我错在哪里以及为什么,还请我如何克服这个问题?
我的 server.js 文件:-
const express = require("express");
const fs = require('fs');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
/**
const port= 80;
**/
const hostname= '127.0.0.1';
const port= 5500;
const path = require('path');
const app = express();
app.get('/register.html',(req,res) => {
res.sendFile(__dirname + '/register.html')
})
/**app.use(express.static('./'));
**/
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
sending();
function sending(){
const transporter = nodemailer.createTransport({
service:'gmail',
auth:{
user:'collectnis@gmail.com',
pass:'free_fire123'
}
});
function sendEmail(mail)
{
var mailOptions= {
from: 'collectnis@gmail.com',
to:mail.to,
subject: mail.subject,
html: mail.body
}
transporter.sendMail(mailOptions, function(err, info) {
if(err){
console.log(err+" "+mail.to)
}
else {
console.log("Email sent: "+info.response)
}
})
}
app.post('/register.html', (req,res)=> {
mail= {
to:req.body.to_address,
subject:"Sheets_Wrap - project details",
body:req.body.NAME + " ------------/////////////------------ "+ req.body.phone + " ------------/////////////------------ " + req.body.email_body + " ------------/////////////------------ " + req.body.code
}
sendEmail(mail)
res.redirect('/register.html')
})
app.listen(port, ()=> {
console.log('SERVER IS RUNNIG AT PORT 8000')
console.log(`Server runnung at http://localhost:${port}/register.html`);
})
}
解决方案
问题:您的 Node.js 应用程序未正确配置为绑定到 Heroku 通过 $PORT 环境变量提供的端口以外的端口。
Heroku dynos 公开了一个动态端口供您的应用绑定。该值在 $PORT 环境变量中公开。您必须更改代码以绑定到此端口。例如:
//Replace your hardcoded port to process.env.PORT
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Our app is running on port ${ PORT }`);
});
这将使用 $PORT 环境变量(如果可用),或者回退到默认端口(对本地开发有用)。请注意,在 Heroku 上浏览您的应用程序时,您仍然使用端口 80 ([your-application].herokuapp.com),而不是您的进程绑定的端口。
推荐阅读
- html - 需要在 R 中使用 rvest 提取以下没有清晰 xpath 的文本
- apache-spark - parquet 如何确定使用哪种编码?
- html - 我怎样才能把特色图片拉低一点?
- typescript - 两种类型之间的 Typescript 类型安全转换
- java - 从 xml 中返回值,xml 迭代
- r - 如何让 plotly::ggplotly 不将 boxplot x 轴标签转换为数值
- python - 如何在不使用 alert.accept() 的情况下使用 Selenium 在 Brave/Chrome 中禁用弹出窗口/警报?
- c# - y轴上的相机旋转值按时间获得高浮点数
- python - bulk_create 内部的函数 - 列表理解
- c++ - 线程 1:EXC_BAD_ACCESS (code=1, address=0x0) 在尝试 mergeSort 时崩溃