简体   繁体   中英

Heroku angular node.js app not connecting to mlab mongodb database

My Heroku app launches and works just fine until I try to do anything that interacts with its database, at which point I get an error of "503- service unavailable."

I have installed the mLab add-on in my resources tab and added the proper MONGODB_URI connection string in the config vars section of my settings tab, though I'm not sure whether it's redundant to do both of those.

When I launch the app with localhost it works fine, though I notice it's still using my local database instead of the mLab one.

Here is my server.js file:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}))
var path = require('path');
app.use(express.static(path.join(__dirname, './dist/public/')));
app.get('*', function(req,res){
res.sendFile(path.join(__dirname, 'dist'));
});
require('./server/config/mongoose.js');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = process.env.MONGOLAB_URI;

MongoClient.connect(url, function (err, db) {
if (err) {
  console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
  console.log('Connection established to', url);
};
var routes_setter = require('./server/config/routes.js');
routes_setter(app);
app.listen(app.get('port'), function(){
console.log('Node app is running on port', app.get('port'))
});
})

And here is my package.json:

{
  "name": "public",
  "version": "0.0.0",
  "main": "server.js",
  "scripts": {
    "nodemon": "nodemon server.js",
    "ng": "ng",
    "start": "node server.js",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot -prod"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/ActionPackedJack/Restaurant-Schedule"
  },
  "engines": {
    "node": "11.2.0",
    "npm": "6.4.1"
  },
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/cli": "~6.0.8",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "body-parser": "^1.18.3",
    "core-js": "^2.5.4",
    "express": "^4.16.4",
    "mongodb": "^3.1.10",
    "mongoose": "^5.3.12",
    "nodemon": "^1.18.6",
    "rxjs": "^6.0.0",
    "rxjs-compat": "^6.3.3",
    "typescript": "~2.7.2",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "typescript": "~2.7.2",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

Any advice would be greatly appreciated. Thanks so much!

Whitelisting IP addresses on Mlab

  1. Go to cluster

  2. Click tab security

  3. Go to Ip Whitelist

  4. Click on edit

Now yu can whitelist all IPs by clicking “Allow Access From Anywhere” in settings or by providing the whitelist IP: 0.0.0.0/0

it will solve the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM