Friday 25 August 2017

Heroku app keeps on trying to connect to local server when I want it to connect it to a mLab cloud

I deployed my app to Heroku, and created a single-node deployment on mLab to serve as my database.However, whenever I try to use my API in any capacity, I get this error: connection error: { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] This makes no sense to me. I set my MONGODB_URI config variable to the URL of my mLab database.I have a folder called config, and in that folder I have a file called config.js which is this:module.exports = { 'secretKey': '01992-19890-01966-19540', 'mongoUrl' : process.env.MONGODB_URI }; And here is my index.js file where I set up the server and try to connect to the mLab database:const express = require('express'); const passport = require('passport'); const http = require('http'); const morgan = require('morgan'); const LocalStrategy = require('passport-local').Strategy; let path = require('path'); let mongoose = require('mongoose'); let config = require('./config/config'); let bodyParser = require('body-parser'); let recipeRouter = require('./routes/recipeRouter'); let userRouter = require('./routes/userRouter'); const app = express(); // set up DB mongoose.connect(config.mongoUrl); let db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function () { // we're connected! console.log("Connected correctly to server"); }); // Set up App app.use(morgan('combined')); // app.use(bodyParser.json({type: '*/*'})); OLD !!!! // !! NEW !! // app.use(bodyParser.json({ limit: '50mb' })); app.use(bodyParser.urlencoded({ extended: false, limit: '50mb' })); // !! NEW !! // app.use(express.static(path.join(__dirname, 'public'))); app.use('/users', userRouter); app.use('/recipes',recipeRouter); // Set up Server const port = process.env.PORT || 3000; const server = http.createServer(app); server.listen(port); Can anyone help me? This is driving me bonkers.

Submitted August 25, 2017 at 10:16PM by T-Dot1992

No comments:

Post a Comment