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

时间:2018-11-28 03:07:56

标签: node.js angular mongodb heroku mlab

我的Heroku应用程序启动并运行正常,直到尝试执行与其数据库进行交互的任何操作为止,此时我都收到“ 503服务不可用”的错误消息。

我已经在我的资源选项卡中安装了mLab附加组件,并在我的设置选项卡的config vars部分中添加了正确的MONGODB_URI连接字符串,尽管我不确定这样做是否多余。

当我使用localhost启动应用程序时,它运行良好,尽管我注意到它仍在使用本地数据库而不是mLab数据库。

这是我的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'))
});
})

这是我的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"
  }
}

任何建议将不胜感激。非常感谢!

1 个答案:

答案 0 :(得分:0)

在Mlab上将IP地址列入白名单

  1. 进入集群

  2. 点击标签安全性

  3. 转到IP白名单

  4. 单击编辑

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

它将解决问题