-1

Here is my server.js

// tools we need
const express = require('express');
const cors = require('cors');
// mongoose helps connect to mongodb database
const mongoose = require('mongoose');

// setting up server
const app = express();
const PORT = process.env.PORT || 5000;

// cors middeware
app.use(cors());

// helps to parse json
app.use(express.json());

const connection = mongoose.connection;

const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');

// whenever go to that url, it will load everything in the second parameter
app.use('/exercises', exercisesRouter);
app.use('/users', usersRouter);

if (process.env.NODE_ENV === "production") {
    app.use(express.static("./client/build"));
}

//const uri = process.env.ATLAS_URI;
mongoose.connect(process.env.MONGODB_URI || "mongodb://user:password@ds029277.mlab.com:29277/heroku_8npb236z", { 
    useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true 
}
);

// starts listening and starts the server
app.listen(PORT, () => {
    console.log(`Server is running on port: ${PORT}!`);
});

It works locally and connects to MLab but when I deploy to Heroku, it just shows the react frontend and does not connect to the MLab server

Here is my package.json in the client folder

{
  "name": "exercises",
  "version": "1.0.0",
  "private": true,
  "main": "./src/index.js",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
      "nodemon": "^2.0.0"
  },
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.7.9",
    "nodemon": "^1.19.4"
  },
  "proxy": "http://localhost:5000"
}

and my package.json server side

{
  "name": "mern-exercise-tracker",
  "version": "0.1.0",
  "main": "server.js",
  "dependencies": {
    "axios": "^0.19.0",
    "bootstrap": "^4.3.1",
    "concurrently": "^5.0.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "if-env": "^1.0.4",
    "mongoose": "^5.7.12",
    "react": "^16.11.0",
    "react-datepicker": "^2.10.0",
    "react-dom": "^16.11.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.2.0",
    "yarn": "^1.19.1"
  },
  "scripts": {
    "start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
    "start:prod": "node server.js",
    "start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
    "client": "cd client && npm run start",
    "seed": "node scripts/seedDB.js",
    "install": "cd client && yarn install",
    "build": "cd client && npm run build",
    "heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gh-pages": "^2.1.1",
    "nodemon": "^2.0.0"
  },
  "engines": {
    "node": "12.13.0"
  }
}

Why am I having this problem? I also have package-lock.json which I do not know what they do.

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
Scott
  • 121
  • 1
  • 2
  • 9

1 Answers1

0

Here you need to enter your user-name and password:

mongodb://user:password@ds029277.mlab.com:29277/heroku_8npb236z

mongodb://test:test123@ds029277.mlab.com:29277/heroku_8npb236z
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Ariful hasan
  • 313
  • 3
  • 9
  • I entered my user and password on the link I just took it out for the sake of this post – Scott Nov 28 '19 at 03:11