Using the Database? - URL Shortener Microservice project

I’m working on the URL Shortener project, but I’m having trouble connecting with the database. I’m not really sure if it’s connected properly or not.

Long story short: Is there a way to test if the database is connected properly?

This is what I have so far. I’m kindof stuck on Steps 8 and 9:

Step 1: Go to mLabs and create a database. Get the MongoDB URI.

Step 2: Put the MongoDB URI into the .env file:
MONGO_URI=mongodb://(user)(password)@(number).mlab.com:(number)/url_shortener_database

Step 3: In the server.js, require the necessary packages:

var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);

Step 4: Create a model or Schema to submit to the database…

var urlSchema = new mongoose.Schema({
      orig_url: { type: String, required: true },
      short_url: Number
  });
var URL = mongoose.model('URL', urlSchema);

Step 5: When the user posts the URL:

app.post("/api/shorturl/new", function (req, res) {
  (Step 6)
  (Step 7)
  (Step 8)
  (Step 9)
});

Step 6: It uses a Regex and removes the HTTPS.

Step 7: And then checks the DNS if it’s a valid website.

Step 8: Go into the database and check that the URL has not already been submitted.

Step 9: If it’s a new URL, submit the URL to the database and return with the ID value.

URL Shortener Microservice:

I only have a second. As to checking if mongoose is running properly, connect returns a promise, so I often do something like:

mongoose.connect(process.env.MONGODB)
.then(
  () => {
    console.log("mongo opened:", process.env.MONGODB)
    runningMongo = true
    confirmRunning()
  },
  err => {
    console.error("### error starting mongo:", process.env.MONGODB)
    console.error(err)
  }
)

I also do the same for the server, so I use:

// starting app and DB

let runningExpress = false
let runningMongo = false

const confirmRunning = () => {
  if (runningExpress && runningMongo && process.env.MODE === 'DEV') {
    console.log("\n*** Server and DB now running. You can confirm it by checking url:\n")
    console.log(process.env.SERVER_URL + ":" + process.env.PORT + "/test")
    console.log("")
  }
}

const message = () => (
  "<h1>Server</h1>" +
  "<h2>Server running: " + true +
  "<h2>Database running: " + runningMongo +
  "<h2>Happy hunting!<h2/>"
)
// test route
app.get("/test", (req, res) => {
  res.send(message())
})

// start app
app.listen(process.env.PORT)
.on('listening', () => {
  console.log("server listening on port:", process.env.PORT)
  runningExpress = true
  confirmRunning()
})
.on('error', (err) => {
  console.error("### error opening port:", process.env.PORT)
  console.error(err)
})

// start mongo
mongoose.connect(process.env.MONGODB)
.then(
  () => {
    console.log("mongo opened:", process.env.MONGODB)
    runningMongo = true
    confirmRunning()
  },
  err => {
    console.error("### error starting mongo:", process.env.MONGODB)
    console.error(err)
  }
)
1 Like

That worked, thank you!

At first it wasn’t connecting to the database, so I compared this one with the one from the lessons. I’d forgot to add a user to the new database, so it was throwing Error 18.