简体   繁体   English

Heroku Angular Node.js应用程序未连接到MLAB MongoDB数据库

[英]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." 我的Heroku应用程序启动并运行正常,直到尝试执行与其数据库进行交互的任何操作为止,此时我都收到“ 503服务不可用”错误。

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. 我已经在我的资源选项卡中安装了mLab附加组件,并在我的设置选项卡的config vars部分中添加了正确的MONGODB_URI连接字符串,尽管我不确定执行这两个操作是否多余。

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. 当我使用localhost启动应用程序时,它运行良好,尽管我注意到它仍在使用本地数据库而不是mLab数据库。

Here is my server.js file: 这是我的server.js文件:

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: 这是我的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 将Mlab上的IP地址列入白名单

  1. Go to cluster 前往丛集

  2. Click tab security 点击标签安全性

  3. Go to Ip Whitelist 前往IP白名单

  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 现在,您可以通过单击设置中的“允许从任何地方访问”或通过提供白名单IP来将所有IP列入白名单: 0.0.0.0/0

it will solve the problem 它会解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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