我的 Heroku 页面上的应用程序错误,即使我的应用程序构建成功

Application Error on my Heroku page, even my app build succeeded

创建于 MongoDB/Atlas 数据库系统。我在本地成功 运行 我的应用程序,但是当我将应用程序推送到 Heroku 时,我收到一条应用程序错误消息:

Application error

An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command

heroku logs --tail

当我查看日志时:

2021-05-10T13:47:26.214536+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-10T13:47:30.291660+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-05-10T13:47:34.377424+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2021-05-10T13:47:34.377459+00:00 app[web.1]: designed for a production environment, as it will leak
2021-05-10T13:47:34.377460+00:00 app[web.1]: memory, and will not scale past a single process.
2021-05-10T13:47:34.430435+00:00 app[web.1]: /app/node_modules/passport-oauth2/lib/strategy.js:86
2021-05-10T13:47:34.430437+00:00 app[web.1]: if (!options.clientID) { throw new TypeError('OAuth2Strategy requires a clientID option'); }
2021-05-10T13:47:34.430438+00:00 app[web.1]: ^
2021-05-10T13:47:34.430439+00:00 app[web.1]:
2021-05-10T13:47:34.430439+00:00 app[web.1]: TypeError: OAuth2Strategy requires a clientID option
2021-05-10T13:47:34.430439+00:00 app[web.1]: at Strategy.OAuth2Strategy (/app/node_modules/passport-oauth2/lib/strategy.js:86:34)
2021-05-10T13:47:34.430440+00:00 app[web.1]: at new Strategy (/app/node_modules/passport-google-oauth20/lib/strategy.js:52:18)
2021-05-10T13:47:34.430440+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:60:14)
2021-05-10T13:47:34.430441+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)
2021-05-10T13:47:34.430441+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-05-10T13:47:34.430442+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-05-10T13:47:34.430442+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-05-10T13:47:34.430443+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
2021-05-10T13:47:34.430443+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-05-10T13:47:34.513199+00:00 heroku[web.1]: Process exited with status 1
2021-05-10T13:47:34.609472+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-10T13:47:35.401133+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=radiant-lowlands-39265.herokuapp.com request_id=04ac407e-ed22-4211-8600-a326bdc1d6c8 fwd="82.222.132.243" dyno= connect= service= status=503 bytes= protocol=https
2021-05-10T13:47:35.696159+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=radiant-lowlands-39265.herokuapp.com request_id=11a57d28-d3c5-4a7a-8f2d-6187ac3997ec fwd="82.222.132.243" dyno= connect= service= status=503 bytes= protocol=https
2021-05-10T14:08:33.993147+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=radiant-lowlands-39265.herokuapp.com request_id=6d9f98e3-cfe2-40b2-a68e-bbac56a743f2 fwd="82.222.132.243" dyno= connect= service= status=503 bytes= protocol=https
2021-05-10T14:08:34.290006+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=radiant-lowlands-39265.herokuapp.com request_id=0c1a084b-0286-4204-8e13-3413bf93e5ac fwd="82.222.132.243" dyno= connect= service= status=503 bytes= protocol=https
2021-05-10T14:14:27.859879+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=radiant-lowlands-39265.herokuapp.com request_id=121f0bcf-5253-40d3-91fb-d5421ec0e4d8 fwd="82.222.132.243" dyno= connect= service= status=503 bytes= protocol=https
2021-05-10T14:14:28.345855+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=radiant-lowlands-39265.herokuapp.com request_id=44bd06c7-dbee-48f3-8389-211ccd2d3ee1 fwd="82.222.132.243" dyno= connect= service= status=503 bytes= protocol=https

运行在我的计算机上运行时,Procfile 或其他一些错误没有问题。我试图用我的 heroku 网址更改 app.js 和 Google Cloud Platform 中的 GoogleStrategy callbackURL 地址。但它不再起作用。我想不通。

提前感谢您的帮助。

问题是您的应用找不到 Google Client ID

TypeError: OAuth2Strategy requires a clientID option

您似乎没有在 Heroku 中正确配置 .env 变量。解决方法其实很简单。

  1. DOTENV Approach

如果您以这种方式定义了 .env

MONGO_URL="xxx.....xxx"
GOOGLE_CLIENT_ID="xx......xxx"
GOOGLE_CLIENT_SECRET="xxx.....xxx"

并且在您的 passport-setupdb-setup 中您使用 dotenv 作为,

require('dotenv').config();
.
.
.
passport.use(
  new GoogleStrategy
  (
    {
      clientID : process.env.GOOGLE_CLIENT_ID,
      clientSecret : process.env.GOOGLE_CLIENT_SECRET,
      callbackURL : '/auth/google/callback'
    },
.
.
.

那么,您必须在计算机上的项目根目录中声明了一个 .env 文件。

您的 .gitignore 文件可能提到了 .env,因为这是执行此操作的标准方法(您不会将环境变量推送到 public/team 存储库)。

它在您的计算机上运行的原因是,该应用程序具有环境变量的上下文。在 Heroku 中,由于 .env 文件不存在,应用程序不知道在哪里可以找到它们。

您所要做的就是将环境变量添加到 Heroku。只需转到 Heroku Dashboard 中的设置,然后转到 Config variables 菜单。以如下所示的格式添加环境变量。重启测功机。

这将解决您的问题。

  1. NON-DOTENV Approach

如果您使用任意文件,例如。 keys.js 或您要从中导出相应机密的内容。例如,

const keys = require('../config/keys');
.
.
.
passport.use(
  new GoogleStrategy
  (
    {
      clientID : keys.googleClientID,
      clientSecret : keys.googleClientSecret,
      callbackURL : '/auth/google/callback'
    },
.
.
.

如果您使用的配置文件类似于 keys.js 等,请检查 Heroku 上是否存在该文件。

如果您使用的是 dotenv,我相信这个答案会解决您的问题。如果你没有使用 dotenv,我会给你一个更新的答案。

(奖金)

对于警告,

Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process.

您可以使用 cookie-session(客户端会话)而不是服务器端会话。