使用node.js&部署到Heroku; express - 如何设置与mongoLab的连接?

时间:2017-07-25 01:04:38

标签: node.js mongodb express heroku mlab

好的,第一次在这里发帖,所以我希望得到一些反馈。

  • 我有两个我已成功上传的馆藏 mongoLab / mLab,他们的名字是" allTest"和" myCollection"。

  • 我想将我的网站部署到heroku(我已经成功完成了 已经)并且能够从mLab中提取和显示数据。

  • 当我在本地跑步和拉动时,一切都很好 来自mongoDB,所以我知道它,因为我想从mLab而不是我的本地mongoDB实例。但是,当我在Heroku上部署时,我对如何设置与mLab的连接感到困惑。

  • 我已经阅读了一些我应该使用URI的文档(我在下面列出), 但我不确定我的代码中还应该改变什么。

这是我到目前为止(我在URI中编辑了我的用户名和密码):

var express = require("express");
var mongojs = require("mongojs");
var logger = require("morgan");
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser());


 var uri = 'mongodb://USERNAME:PASSWORD@ds157702.mlab.com:57702/heroku_r45mjcns';


app.use(express.static("public"));


var databaseUrl = "mydb";
var collections = ["myCollection"];

var databaseUrl2 = "testMenu";
var collections2 = ["allTests"];


var db = mongojs(databaseUrl, collections);
var db2 = mongojs(databaseUrl2, collections2);


app.get("/", function(req, res) { 
   res.sendfile('public/home.html');
});


app.get("/find2/:id", function(req, res) {
  var test_id2 = req.param('id');



  db2.allTests.find({"TestName": {$regex:test_id2, $options: 'i'} }, function(error, found) {
    // Log any errors if the server encounters one
    if (error) {
      console.log(error);
    }
    // Otherwise, send the result of this query to the browser
    else {
      res.json(found);
      console.log("From server");
      console.log(found);

    }
  });
  });




app.get("/find/:id", function(req, res) {
  var test_id = req.param('id');

  db2.allTests.find({"Test": test_id }, function(error, found) {
    // Log any errors if the server encounters one
    if (error) {
      console.log(error);
    }
    // Otherwise, send the result of this query to the browser
    else {
      res.json(found);
      console.log("From server");
      console.log(found);

    }
  });
  });





  app.get("/find/dept/:id", function(req, res) {
  var test_id = req.param('id');

  db.myCollection.find({ $or: [ { "WebsiteCategory": test_id }, { "WebsiteCategory2": test_id } ] }, function(error, found) {
    // Log any errors if the server encounters one
    if (error) {
      console.log(error);
    }
    // Otherwise, send the result of this query to the browser
    else {
      res.json(found);
      console.log("From server");
      console.log(found);

    }
  });
  });







  app.get("/find/dept2/:id", function(req, res) {
  var test_id = req.param('id');

  db.myCollection.find({"TestSet": test_id }, function(error, found) {
    // Log any errors if the server encounters one
    if (error) {
      console.log(error);
    }
    // Otherwise, send the result of this query to the browser
    else {
      res.json(found);
      console.log("From server");
      console.log(found);

    }
  });
  });







// Set the app to listen on port 3000
app.listen(process.env.PORT || 3000);

`

具体来说,我正在尝试的这段代码:

     app.get("/find/dept/:id", function(req, res) {
  var test_id = req.param('id');

  db.myCollection.find({ $or: [ { "WebsiteCategory": test_id }, { "WebsiteCategory2": test_id } ] }, function(error, found) {
    // Log any errors if the server encounters one
    if (error) {
      console.log(error);
    }
    // Otherwise, send the result of this query to the browser
    else {
      res.json(found);
      console.log("From server");
      console.log(found);

    }
  });
  });

任何帮助都将不胜感激!!

0 个答案:

没有答案