【问题标题】:App hosted on Heroku failing to load web pageHeroku 上托管的应用程序无法加载网页
【发布时间】:2018-12-04 08:44:48
【问题描述】:

我目前正在开发一个使用 express 来提供网页的 nodejs 应用程序。它还兼作不和谐机器人。当我在 localhost 上运行应用程序时,discord 和网页部分都可以正常工作。但是,当我在 Heroku 上托管它时,discord 机器人工作正常,但是当我查看实时网页时网页无法加载。

这是我的 index.js 代码。

const fs = require('fs'), discord = require('discord.js'), express = require('express'), crypto = require('crypto'), bodyParser = require('body-parser'), path = require('path'), archiver = require('archiver')
const goonsUp = require(__dirname + '/commands/goonsUp/goonsUp')
const roastMe = require(__dirname + '/commands/roastMe/roastMe')

var config = JSON.parse(fs.readFileSync('config.json')),
client = new discord.Client()

roastMe(client, discord)
goonsUp(client, discord)
client.login(config.token)

var app = express()
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.set('view engine', 'ejs')

app.get('/', (req, res)=>{
    client.channels.get('457201559772594211').send('Test')
    res.render('fileReq')
})

app.post('/', urlencodedParser, (req, res)=>{
    var hash = crypto.createHmac('sha256', 
    req.body.password).digest('hex')
if(hash==='709f8df16dca8b307a2a8ea13356eb1f3e138f117eb6592e4e030478bef1bb04'){
    var downloads = JSON.parse(fs.readFileSync(__dirname + '/download.json'))
    var archive = archiver('zip')
    var output = fs.createWriteStream(__dirname + '/downloads.zip')
    archive.pipe(output)

    for(var i = 0; i < downloads.length; i++){
        var path = downloads[i]
        archive.append(fs.readFileSync(path), {name: path.replace(/^.*[\\\/]/, '')})
    }

        archive.finalize()
        setTimeout(()=>{
            res.download(__dirname + '/downloads.zip')
        }, 2000)
    }
})

app.listen(process.env.PORT || 3000, function(){

})

据我所知,根据我的测试,url 请求甚至没有到达应用程序。我可以说出来,因为每当请求页面时,我都会让应用程序报告不和谐,而且它在 localhost 上做得很好。但是,当在 heroku 上运行时,查看实时网页从未调用过代码,并且该页面出现了应用程序错误。然而,该机器人的其他元素似乎确实可以在 Heroku 中使用。

有人知道为什么会这样吗?谢谢。

编辑: 这是日志上发布的错误

2018-06-27T04:49:32.337810+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=chiraag-discord-bot.herokuapp.com request_id=c737acac-7313-4ece-8359-d3bca72e1cc0 fwd="24.5.143.134" dyno= connect= service= status=503 bytes= protocol=https
2018-06-27T04:49:32.981684+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=chiraag-discord-bot.herokuapp.com request_id=ec9c3cf8-3af9-4bcc-b1b3-bb84bb6c958c fwd="24.5.143.134" dyno= connect= service= status=503 bytes= protocol=https

【问题讨论】:

  • 查看日志时,您在 heroku 上收到的错误消息是什么?
  • 我会将日志发布到问题中。

标签: javascript node.js express heroku discord.js


【解决方案1】:

过去我在 Heroku 和 Node.js 上遇到过同样的问题。为我解决的问题是将端口号“OR”语句更改为 8080。据我了解,端口 8080 是在 80 不能使用时使用的辅助号码。代码更改很简单,如下面的 sn-p 所示。

app.listen(process.env.PORT || 8080, function(){})

【讨论】:

  • 我试过这个,但打开网页时仍然出现同样的错误。我什至尝试在 url 后面加上 :8080,但仍然出现同样的错误
猜你喜欢
  • 2020-01-19
  • 2021-02-24
  • 2019-08-15
  • 2019-01-10
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 2016-10-28
相关资源
最近更新 更多