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: