0

我正在尝试将我的项目部署到heroku,但这就是我得到的,我已经尽我所能但仍然这个问题请帮助我,我的package.JSON

     {
      "name": "storybooks",
      "version": "1.0.0",
      "description": "App for creating public and private stories",
      "main": "app.js",
      "scripts": {
        "start": "cross-env NODE_ENV=production node app",
        "dev": "cross-env NODE_ENV=development nodemon app",
        "postinstall": "node ./scripts/post-install.js"
      },
      "author": "Dayan",
      "license": "MIT",
      "dependencies": {
        "all": "^0.0.0",
        "connect-mongo": "^3.2.0",
        "dotenv": "^8.2.0",
        "ejs": "^3.1.6",
        "express": "^4.17.1",
        "express-handlebars": "^4.0.4",
        "express-session": "^1.17.1",
        "fs-extra": "^9.0.1",
        "method-override": "^3.0.0",
        "moment": "^2.27.0",
        "mongoose": "^5.9.19",
        "morgan": "^1.10.0",
        "passport": "^0.4.1",
        "passport-google-oauth20": "^2.0.0"
      },
      "devDependencies": {
        "cross-env": "^7.0.3",
        "nodemon": "^2.0.4"
      }
    }

我的文件结构

我将不胜感激

这是我的 .env

PORT = 3000
MONGO_URI = mongodb+srv://<Myuserhere>@cluster0.sjptc.mongodb.net/storyBooks
GOOGLE_CLIENT_ID = ##
GOOGLE_CLIENT_SECRET = ##

我的数据库文件

const mongoose = require('mongoose')

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
    })

    console.log(`MongoDB Connected: ${conn.connection.host}`)
  } catch (err) {
    console.error(err)
    process.exit(1)
  }
}

module.exports = connectDB

我的护照文件

const GoogleStrategy = require('passport-google-oauth20').Strategy
const mongoose = require('mongoose')
const User = require('../models/User')

module.exports = function (passport) {
  passport.use(
    new GoogleStrategy(
      {
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        callbackURL: '/auth/google/callback',
      },
      async (accessToken, refreshToken, profile, done) => {
        const newUser = {
          googleId: profile.id,
          displayName: profile.displayName,
          firstName: profile.name.givenName,
          lastName: profile.name.familyName,
          image: profile.photos[0].value,
        }

        try {
          let user = await User.findOne({ googleId: profile.id })

          if (user) {
            done(null, user)
          } else {
            user = await User.create(newUser)
            done(null, user)
          }
        } catch (err) {
          console.error(err)
        }
      }
    )
  )

  passport.serializeUser((user, done) => {
    done(null, user.id)
  })

  passport.deserializeUser((id, done) => {
    User.findById(id, (err, user) => done(err, user))
  })
}

所以这是文件的代码,用于包 json、passport.js、db.js、config.env 但我无法弄清楚我得到的错误,我在开发人员的帮助下解决了另一个错误但是这个是我得到的**MongoParseError: Invalid connection string**

4

1 回答 1

0

cross-env是开发依赖。Heroku 默认只安装生产依赖。这就是它抛出该错误的原因。

除此之外,我不建议您在 heroku 上使用开发服务器,如果您运行服务器(我看到了明确的依赖关系),您需要像node app.json一样运行 app.jsnpm start

于 2021-08-06T05:15:05.093 回答