1

I would appriciate any help you can give me on this matter. I am trying to deploy a very basic database to Heroku using MongoDB Atlas, but on my Heroku project I'm only getting HTTP ERROR 500. (For developing purposes I have a local mongoose database that works fine). I think the problem is with process.env.PORT but I'm not totally sure.

here is how my index.js looks like:

const express = require("express");
require("./mongoose");
const cors = require("cors");
require('dotenv').config(); //for setting environment variables on server
const QuestionRouter = require('./routers/QuestionsRouter');
const UserRouter = require('./routers/UserRouter');

const app = express();

app.use(cors());
const port = process.env.PORT || 7000;
console.log(process.env);
const host = '0.0.0.0';

//customize our server
app.use(express.json()) //automatically parse incoming json to an object so we can access it in our req handlers
app.use(UserRouter);
app.use(QuestionRouter);

app.listen(port,host, () => {
    console.log("server up on port " + port);
})

this is my mongoose.js file:

const mongoose = require('mongoose');
let MONGODB_URI = process.env.ATLAS_URI || "mongodb://127.0.0.1:27017/databasename";

mongoose.connect(MONGODB_URI, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
})

and this is how my json looks like:

{
  "name": "backend",
  "version": "1.0.0",
  "engines": {
    "node": "13.12.0"
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongodb": "^3.6.5",
    "mongoose": "^5.12.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

I basically went through all of the solutions people came up with here- Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch) but nothing works.

  • I defined a ATLAS_URI config var on Heroku with my Atlas database uri: mongodb+srv://meli:@cluster0.mq5lg.mongodb.net/MYDATABASENAME?retryWrites=true&w=majority
  • I also have a .env file on the root of my project where I define ATLAS_URI.
  • I have a Procfile file with "web: node index.js" command.

Thank you for any input!

liooshine
  • 105
  • 6

0 Answers0