Heroku Application doesn't work when I try to deploy MERN app

Viewed 28

I'm trying to figure out a last assignment for my boot camp and am close to running out of time.

Heroku has an application error and I can't figure out how to fix it. I've tried deleting and reinstalling node modules, adding node and npm versions in my package.json, npm update, heroku restart, changing my PORT and have rewritten my connection and server files several times. I'm losing track of what I've tried. I think I might be missing something when it comes to MongoDB or Apollo, both of which I'm required to use, but I don't know what. I added keys to my .env and I don't know if I've done it wrong or something but it just won't work on Heroku. It works locally.

Here is my package.json currently.

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "watch": "nodemon server.js"
  },
  "keywords": [],
  "author": "Wren Sanchez",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.0",
    "express": "^4.17.1",
    "dotenv": "^16.0.1",
    "graphql": "^15.5.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.9.10",
    "apollo-server-express": "^3.6.1",
    "nodemon": "^2.0.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.3"
  }
}

My server.js

const { ApolloServer } = require("apollo-server-express");
const express = require('express');
const path = require('path');
const db = require('./config/connection');
const { typeDefs, resolvers } = require("./schemas");
const { authMiddleware } = require('./utils/auth');
require('dotenv').config();

const app = express();
const PORT = process.env.PORT || 3001;

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: authMiddleware,
  cache: "bounded",
});

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

if (process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, '../client/build')));
}

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, '../client/build/index.html'));
});

const startApolloServer = async (typeDefs, resolvers) => {
  await server.start();
  server.applyMiddleware({ app });

  db.once("open", () => {
    app.listen(PORT, () => {
      console.log(`API server running on port ${PORT}!`);
      console.log(
        `Use GraphQL at http://localhost:${PORT}${server.graphqlPath}`
      );
    });
  });

startApolloServer(typeDefs, resolvers);
}

My connection.js

const mongoose = require('mongoose');
require('dotenv').config({ path: '../.env' });

mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/googlebooks', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

module.exports = mongoose.connection;

And if useful, my repository: https://github.com/wrenvana/Book-Search-Engine

Any help is appreciated, we have been learning React and MERN at the very end and I've been having a hard time with it.

0 Answers
Related