简体   繁体   中英

Deploying a MongoDB database

I'm building a small Webapp. I use MongoDB to retrieve and store my data, at the moment everything is working perfectly fine, but now I want to host my app online.

I know how to deploy my script on Heroku, but I don't know how it should work with MongoDB, maybe adding MongoDB to my app's requirements?

I made some research and found about Mlab, there is an Heroku add-on for it but sadly Mlab is going to migrate to Atlas soon

(edited, just say you added about mlab being migrated, AWS also offers the same sandbox options as mlab on atlas as you said, you can also install it locally and add it to your settings.py)

local setup

  1. download a free MongoDB database at https://www.mongodb.com
  2. PyMongo Python needs a MongoDB driver to access the MongoDB database. use PIP to install "PyMongo"

mlab sample setup

在此处输入图片说明 why don't you have a look at one of my projects and see how i deployed mongo on a CDN

I used this site to make a sandboxed FREE CDN database https://mlab.com/

stick with this line where we are using the database from a CDN, this way we don't even need to install it on our system!

// this is our MongoDB database, change this to 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}`));

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