【问题标题】:Deploying a MongoDB database部署 MongoDB 数据库
【发布时间】:2019-05-06 21:55:59
【问题描述】:

我正在构建一个小型 Web 应用程序。我使用 MongoDB 来检索和存储我的数据,目前一切正常,但现在我想在线托管我的应用程序。

我知道如何在 Heroku 上部署我的脚本,但我不知道它应该如何与 MongoDB 一起工作,也许将 MongoDB 添加到我的应用程序的需求中?

我做了一些关于 Mlab 的研究,发现它有一个 Heroku 插件,但遗憾的是 Mlab 很快就会迁移到 Atlas

【问题讨论】:

  • 能否指定您使用的是 django/flask 还是什么库?
  • @Mr-Programs 我正在使用 Dash!

标签: python mongodb heroku


【解决方案1】:

(已编辑,只是说您添加了关于正在迁移的 mlab,AWS 还提供了与您所说的 atlas 上的 mlab 相同的沙箱选项,您也可以将其安装在本地并添加到您的 settings.py)

本地设置

  1. https://www.mongodb.com下载免费的MongoDB数据库
  2. PyMongo Python 需要一个 MongoDB 驱动程序来访问 MongoDB 数据库。使用 PIP 安装“PyMongo”

mlab 示例设置

为什么不看看我的一个项目,看看我是如何在 CDN

上部署 mongo

我用这个网站做了一个沙盒FREE CDN数据库https://mlab.com/

在我们使用 CDN 数据库的地方坚持这一行,这样我们甚至不需要在我们的系统上安装它!

// 这是我们的 MongoDB 数据库,将其更改为 CLOUD MONGO link const dbRoute = "mongodb://*****:******@******.mlab.com:*****/*********";

const mongoose = require("mongoose");
const express = require("express");
const bodyParser = require("body-parser");
const logger = require("morgan");
const Data = require("./data");

const API_PORT = 3001;
const app = express();
const router = express.Router();

// this is our MongoDB database, change this to CLOUD MONGO link
const dbRoute = "mongodb://*****:******@******.mlab.com:*****/*********";

// connects our back end code with the database
mongoose.connect(
  dbRoute,
  { useNewUrlParser: true }
);

let db = mongoose.connection;

db.once("open", () => console.log("connected to the database"));

// checks if connection with the database is successful
db.on("error", console.error.bind(console, "MongoDB connection error:"));

// (optional) only made for logging and
// bodyParser, parses the request body to be a readable json format
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger("dev"));

// this is our get method
// this method fetches all available data in our database
router.get("/getData", (req, res) => {
  Data.find((err, data) => {
    if (err) return res.json({ success: false, error: err });
    return res.json({ success: true, data: data });
  });
});

// this is our update method
// this method overwrites existing data in our database
router.post("/updateData", (req, res) => {
  const { id, update } = req.body;
  Data.findOneAndUpdate(id, update, err => {
    if (err) return res.json({ success: false, error: err });
    return res.json({ success: true });
  });
});

// this is our delete method
// this method removes existing data in our database
router.delete("/deleteData", (req, res) => {
  const { id } = req.body;
  Data.findOneAndDelete(id, err => {
    if (err) return res.send(err);
    return res.json({ success: true });
  });
});

// this is our create methid
// this method adds new data in our database
router.post("/putData", (req, res) => {
  let data = new Data();

  const { id, message } = req.body;

  if ((!id && id !== 0) || !message) {
    return res.json({
      success: false,
      error: "INVALID INPUTS"
    });
  }
  data.message = message;
  data.id = id;
  data.save(err => {
    if (err) return res.json({ success: false, error: err });
    return res.json({ success: true });
  });
});

// append /api for our http requests
app.use("/api", router);

// launch our backend into a port
app.listen(API_PORT, () => console.log(`LISTENING ON PORT ${API_PORT}`));

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2020-07-19
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2014-05-01
    相关资源
    最近更新 更多