托管在 Heroku 上的应用程序无法加载网页



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

这是我的索引.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和Node上遇到过同样的问题。为我修复它的是将端口号"OR"语句更改为 8080。据我了解,端口 8080 是 80 不能时使用的辅助号码。代码更改非常简单,如下面的代码片段所示。

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

最新更新