部署Node.js应用程序时出现Heroku错误H10

fcwjkofz  于 2022-11-13  发布在  Node.js
关注(0)|答案(1)|浏览(178)

我一直在努力将我的应用程序部署到Heroku,但不断遇到H10错误。应用程序在本地工作,但部署到Heroku没有运气。
下面是index.js:

import express from 'express'
import 'dotenv/config'
import router from './routes/index.js'
import morgan from 'morgan'

const app = express()

const port = process.env.PORT || 5000

app.use('/public', express.static('public'))

app.use(express.urlencoded({ extended: false }))

app.use(express.json())

app.use(morgan('dev'))

app.use('/', router)

app.listen(process.env.PORT || port)

下面是我的过程文件:

web: node index.js

下面是package.json:

{
  "name": "node-sso-example-app",
  "version": "1.0.0",
  "description": "Example Node.js SSO App using WorkOS",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "author": "WorkOS",
  "dependencies": {
    "express": "^4.18.1",
    "ejs": "^3.1.8",
    "router": "^1.3.7",
    "dotenv": "^16.0.1",
    "@workos-inc/node": "^2.11.0",
    "morgan": "^1.10.0",
    "cookie-parser": "^1.4.6",
    "express-session": "^1.17.2",
    "http-errors": "~1.6.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.19"
  },
  "engines": {
    "node": "^16.17.0"
  }
}

我被困在这一点上,并已阅读了什么感觉像每一个堆栈溢出问题对这个相同的错误没有运气。

rks48beu

rks48beu1#

我是nodejs和heroku的新手,但我遇到了同样的问题H10。它可能与端口有关。请尝试完全删除此行

const port = process.env.PORT || 5000

并为index.js添加这个(我相信您可以使用任何端口,Heroku可以手动分配不同的端口

//start server
app.listen(process.env.PORT || 3000, function(){
    console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

或者保留const port,但改用以下代码:

app.listen(port, () => {
    console.log(`server started on port ${port}`);
})

相关问题