Error deploying Mongo atlas to Heroku with node.js

  angular, heroku, mongodb, node.js

I am currently trying to deploy my angular app on Heroku. I have created a Mongo atlas cluster, set all config vars on Heroku and whitelisted the IP address on mongo to 0.0.0.0/0.

My server.js looks like this:

require("dotenv").config();
const cors = require("cors");
const path = require("path");
const express = require("express");
const server = express();
const MongoClient = require("mongodb").MongoClient;
const body_parser = require("body-parser");

// parse JSON (application/json content-type)
server.use(body_parser.json());
server.use(express.json());
server.use(cors());

//const db = require("./db");
const dbConnectionUrl = process.env.MONGO_URL;
const dbName = process.env.dbName;
const collectionName = process.env.collectionName;

function initialize(
  dbName,
  dbCollectionName,
  successCallback
) {
  MongoClient.connect(dbConnectionUrl, function (err, dbInstance) {
    if (err) {
      console.log(`[MongoDB connection] ERROR: ${err}`);
    } else {
      const dbObject = dbInstance.db(dbName);
      const dbCollection = dbObject.collection(dbCollectionName);
      console.log("[MongoDB connection] SUCCESS");

      successCallback(dbCollection);
    }
  });
}

initialize(
  dbName,
  collectionName,
  function (dbCollection) {
    server.get("/workshops/:id", (request, response) => {
      const itemId = request.params.id;

      dbCollection.findOne({ _id: itemId }, (error, result) => {
        if (error) throw error;
        response.json(result);
      });
    });
  },
  function (err) {
    throw err;
  }
);

server.use(express.static(__dirname + "/dist/test"));

server.get("/*", function (req, res) {
  res.sendFile(path.join(__dirname + "/dist/test/index.html"));
});

server.listen(process.env.PORT || 3000, () => {
  console.log(`Server listening at 3000`);
});

Without this code below, it deploys fine to Heroku. But I’m assuming its needed for Mongo?

function initialize(
  dbName,
  dbCollectionName,
  successCallback
) {
  MongoClient.connect(dbConnectionUrl, function (err, dbInstance) {
    if (err) {
      console.log(`[MongoDB connection] ERROR: ${err}`);
    } else {
      const dbObject = dbInstance.db(dbName);
      const dbCollection = dbObject.collection(dbCollectionName);
      console.log("[MongoDB connection] SUCCESS");

      successCallback(dbCollection);
    }
  });
}

initialize(
  dbName,
  collectionName,
  function (dbCollection) {
    server.get("/workshops/:id", (request, response) => {
      const itemId = request.params.id;

      dbCollection.findOne({ _id: itemId }, (error, result) => {
        if (error) throw error;
        response.json(result);
      });
    });
  },
  function (err) {
    throw err;
  }
);

Without this code below, it works locally and I can successfully grab data from Mongo atlas with the endpoint /workshops:id. But without this, it won’t deploy to Heroku successfully

server.use(express.static(__dirname + "/dist/test"));

server.get("/*", function (req, res) {
  res.sendFile(path.join(__dirname + "/dist/test/index.html"));
});

With my current server.js it builds successfully on Heroku, but I get the Application error screen and my logs are all successful. Any advice would be greatly appreciated.

Source: Angular Questions

One Reply to “Error deploying Mongo atlas to Heroku with node.js”

Leave a Reply to enas Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.