简体   繁体   English

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

[英]App hosted on Heroku failing to load web page

I am currently working on a nodejs app which uses express to serve a web page. 我目前正在开发一个使用express服务于网页的nodejs应用程序。 It also doubles as a discord bot. 它也可以作为不和谐的机器人。 When I run the app on localhost, both the discord and web page parts work fine. 当我在localhost上运行该应用程序时,discord和网页部分都可以正常工作。 However, when I host it on Heroku, the discord bot works fine, however the web page fails to load when I view the live web page. 但是,当我在Heroku上托管它时,discord bot可以正常工作,但是当我查看实时网页时网页无法加载。

Here is my index.js code. 这是我的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(){

})

From my tests, as far as I can tell, the url request isn't even getting to the app. 根据我的测试,据我所知,URL请求甚至没有到达应用程序。 I could tell because I made the app report to discord whenever the page was requested, and it did so fine on localhost. 我之所以知道是因为每当有人请求该页面时,我都会将应用报告设为不和谐,并且在localhost上运行得很好。 However, when ran on heroku, viewing the live web page never called the code, and the page comes up with an application error. 但是,当在heroku上运行时,查看实时网页从未调用该代码,并且该页面出现应用程序错误。 Other elements of the bot, however, do appear to work from Heroku. 但是,该机器人的其他元素确实可以在Heroku中使用。

Does anyone know why this may be? 有谁知道为什么会这样吗? Thanks. 谢谢。

EDIT: Here is the error posted on the logs 编辑:这是在日志上发布的错误

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

I have had the same issue in the past with Heroku and Node. 过去,Heroku和Node遇到过相同的问题。 What fixed it for me was changing the port number 'OR' statement to 8080. Port 8080, to my understanding, is a secondary number used when 80 cannot. 对我来说,解决此问题的方法是将端口号“ OR”语句更改为8080。据我所知,端口8080是80无法使用时使用的辅助编号。 The code change would be simple enough as seen in the snippet below. 如下面的代码片段所示,代码更改将非常简单。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM