首页 > 解决方案 > 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`);
})
}

标签: htmlcssnode.jsheroku

解决方案


问题:您的 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),而不是您的进程绑定的端口。


推荐阅读