My app works locally but on Heroku i get the following:
Here is my index.js:
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
//mongoose
mongoose.connect(process.env.URI || 'mongodb://localhost/book_trak', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.catch(err => console.log(err));
var db = mongoose.connection;
db.on('error', (err) => console.log(err));
db.once('open', () => console.log('we are connected'));
const app = express();
//body parser no longer needed
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(cors())
app.use('/book', require('./routes/book'));
app.use('/author', require('./routes/author'));
if(process.env.NODE.ENV === 'production') {
app.use(express.static(path.join(__dirname, 'front_end', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'front_end', 'build', 'index.html'))
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log('running on Port ' + PORT));
package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"engines": {
"node": "12.16.0"
},
"main": "index.js",
"scripts": {
"client-install": "cd front_end && npm install",
"start": "node index",
"server": "nodemon index",
"client": "cd front_end && npm start",
"dev": "concurrently \" npm run server \" \" npm run client\"",
"heroku-postbuild": "cd front_end && npm install && npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.9.7"
},
"devDependencies": {
"concurrently": "^5.1.0",
"nodemon": "^2.0.2"
}
}
my files:
-front_end
----node_modules
----public
----src
----package-lock.json
----package.json
----README.md
-models
-node_modules
-routes
-.env
-.gitignore
-index.js
-package-lock.json
-package.json
I’ve been searching in this forum and stackoverflow all day. I think the problem lies in connecting to the database but I am not sure.