在 Heroku 上成功部署,但使用 Express + Node.JS 时出现应用程序错误

Success deploy on Heroku but I get Application Error using Express + Node.JS

这是我第一次尝试部署,我选择了Heroku,请原谅,我将其部署为一个有趣的项目来练习。

我已按照此网站说明进行操作 deploy Heroku

由于我在终端上看到的内容,我已经成功部署了它

-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/nodejs
-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       
       Resolving node version 14.x...
       Downloading and installing node 14.18.2...
       Using default npm version: 6.14.15
       
-----> Restoring cache
       - node_modules
       
-----> Installing dependencies
       Installing node modules
       
       > ejs@2.7.4 postinstall /tmp/build_b0e24fab/node_modules/ejs
       > node ./postinstall.js
       
       
       > fsevents@1.2.13 install /tmp/build_b0e24fab/node_modules/fsevents
       > node install.js
       
       
       Skipping 'fsevents' build as platform linux is not supported
       
       > nodemon@1.18.5 postinstall /tmp/build_b0e24fab/node_modules/nodemon
       > node bin/postinstall || exit 0
       
       Love nodemon? You can now support the project via the open collective:
        > https://opencollective.com/nodemon/donate
       
       added 458 packages in 4.084s
       
-----> Build
       
-----> Caching build
       - node_modules
       
-----> Pruning devDependencies
       audited 458 packages in 1.789s
       
       32 packages are looking for funding
         run `npm fund` for details
       
       found 4 vulnerabilities (3 moderate, 1 high)
         run `npm audit fix` to fix them, or `npm audit` for details
       
-----> Build succeeded!
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 36.7M
-----> Launching...
       Released v5
       https://kanadb.herokuapp.com/ deployed to Heroku

但是当我尝试访问 Heroku 提供给我的那个 URL 时,它说 应用程序错误 。我试着去日志看看发生了什么。

这是日志告诉我的内容:

2022-01-06T16:21:23.381226+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2022-01-06T16:21:23.381226+00:00 app[web.1]:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2022-01-06T16:21:23.381227+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2022-01-06T16:21:23.381227+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
2022-01-06T16:21:23.381228+00:00 app[web.1]:     at internal/main/run_main_module.js:17:47 {
2022-01-06T16:21:23.381237+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
2022-01-06T16:21:23.381238+00:00 app[web.1]:   requireStack: []
2022-01-06T16:21:23.381238+00:00 app[web.1]: }
2022-01-06T16:21:23.541784+00:00 heroku[web.1]: Process exited with status 1
2022-01-06T16:21:23.625528+00:00 heroku[web.1]: State changed from starting to crashed
2022-01-06T16:32:41.768068+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=kanadb.herokuapp.com request_id=645c04d9-37e3-49d1-9215-7100a50e6ed6 fwd="42.119.203.224" dyno= connect= service= status=503 bytes= protocol=https
2022-01-06T16:32:42.482576+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=kanadb.herokuapp.com request_id=4310b722-24f5-4b2c-98cf-9b7f2d1ab4bb fwd="42.119.203.224" dyno= connect= service= status=503 bytes= protocol=https

这是我的 ExpressJS 代码,它只执行 1 POST route,每当我单击 Front-end 中的项目时,它会向 POST route 发送一个 URL =] 并修改 URL 以提取我需要的数据并插入到我的 Supabase:

var PORT = process.env.PORT || 8000
const axios = require('axios')
const cheerio = require('cheerio')
const express = require('express')
const app = express()
const { createClient } = require('@supabase/supabase-js')
const supabase = createClient(
    'mySupabaseURL',
    'myAnonAPIKey'
  )

let cors = require('cors')
app.use(cors({
    origin: 'http://localhost:3000',
    credentials: true
}))

app.use(express.static("public"))
app.use(express.json())
app.use(express.urlencoded({extended: true}))

app.post('/', async function (req, res, next) {
    let animeData = []
    res.status(201).json({message: 'success'})
    let basedURL = req.body

    await axios(basedURL.animeURL)
        .then(async response =>{
            let html = response.data
            let $ = cheerio.load(html, {xmlMode: true})

            $('script', html).each(function(){
                let animeEpisode = $(this).text()
                animeData.push({
                  animeEpisode
                })
            })
            let result = Object.entries(animeData[15])
            let stringResult = result[0].toString()
            let arrayResult = stringResult.split(",")
            for(let i = 0; i<arrayResult.length; i++){
                let fboLink2 = arrayResult[i]
                let location = fboLink2.indexOf(`source_fbo: [{"file":"`)

                if(location != -1){
                    let newFBO = fboLink2.replace(`source_fbo: [{"file":"`, "")
                    let finalFBO = newFBO.replace(`"}]`, "")
                    const {data} = await supabase
                    .from('anime_detail')
                    .update({anime_video: finalFBO})
                    .match({anime_title: basedURL.animeTitle, anime_url: basedURL.animeURL, anime_episode: basedURL.anime_episode})
                }
            }
        }).catch(err => console.log(err))
  })


app.listen(PORT, ()=> {console.log(`Server is running`)})

当我 Node.JS + Express 和我的 Front-end 在本地 运行 时,一切正常,所以我不知道是什么导致了问题。

这是我的结构,我也插入了 Procfileweb: node index.js

更新一:

使用 web: node ./src/index.js 修复 Procfile 后,需要 index.js 的错误已修复,但它导致我遇到另一个我真的不知道如何修复的问题。

这是它的日志:

2022-01-06T19:22:51.002284+00:00 app[web.1]: internal/modules/cjs/loader.js:905
2022-01-06T19:22:51.002303+00:00 app[web.1]:   throw err;
2022-01-06T19:22:51.002304+00:00 app[web.1]:   ^
2022-01-06T19:22:51.002304+00:00 app[web.1]: 
2022-01-06T19:22:51.002304+00:00 app[web.1]: Error: Cannot find module '@supabase/supabase-js'
2022-01-06T19:22:51.002304+00:00 app[web.1]: Require stack:
2022-01-06T19:22:51.002305+00:00 app[web.1]: - /app/src/index.js
2022-01-06T19:22:51.002305+00:00 app[web.1]:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at Module.require (internal/modules/cjs/loader.js:974:19)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at require (internal/modules/cjs/helpers.js:93:18)
2022-01-06T19:22:51.002306+00:00 app[web.1]:     at Object.<anonymous> (/app/src/index.js:9:26)
2022-01-06T19:22:51.002307+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:1085:14)
2022-01-06T19:22:51.002307+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2022-01-06T19:22:51.002308+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2022-01-06T19:22:51.002308+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
2022-01-06T19:22:51.002308+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
2022-01-06T19:22:51.002309+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
2022-01-06T19:22:51.002309+00:00 app[web.1]:   requireStack: [ '/app/src/index.js' ]
2022-01-06T19:22:51.002309+00:00 app[web.1]: }
2022-01-06T19:22:51.275795+00:00 heroku[web.1]: State changed from starting to crashed
2022-01-06T19:22:48.000000+00:00 app[api]: Build succeeded

Heroku 找不到您的 index.js 文件:

Error: Cannot find module '/app/index.js'

您的 index.js 似乎位于名为 src/ 的目录中。这应该反映在您的 Procfile:

web: node src/index.js