Tell us what’s happening:
I’m done building the url shortener application and when testing both locally and on live server everything works as should but, when I run the test on FCC it fails can someone please assist?
Your project link(s)
solution: URL Shortener
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: Back End Development and APIs Projects - URL Shortener Microservice
Link to the challenge:
Here is my code for the URL Shortner :
const express = require(‘express’);
var mongo = require(‘mongodb’);
var mongoose = require(‘mongoose’);
var bodyParser = require(‘body-parser’);
var shortId = require(‘shortid’);
const app = express();
const port = process.env.PORT || 3000;
var dns = require(‘dns’);
require(‘dotenv’).config();
//Setup DB
//mongoose.connect(process.env.DB_URI)
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
//URL Shortner
// Build a schema and model to store saved URLS
var ShortURL = mongoose.model(‘ShortURL’, new mongoose.Schema({
short_url: String,
original_url: String,
suffix: String
}));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.post(“/api/shorturl/new/”, (req, res) => {
let client_requested_url = req.body.url
let suffix = shortid.generate();
let newShortURL = suffix
let newURL = new ShortURL({
short_url: __dirname + "/api/shorturl/" + suffix,
original_url: client_requested_url,
suffix: suffix
})
newURL.save((err, doc) => {
if (err) return console.error(err);
res.json({
"saved": true,
"short_url": newURL.short_url,
"orignal_url": newURL.original_url,
"suffix": newURL.suffix
});
});
});
app.get(“/api/shorturl/:suffix”, (req, res) => {
let userGeneratedSuffix = req.params.suffix;
ShortURL.find({ suffix: userGeneratedSuffix }).then(foundUrls => {
let urlForRedirect = foundUrls[0];
res.redirect(urlForRedirect.original_url);
});
});
app.get(“/api/shorturl/:suffix”, (req, res) => {
let userSuffix = req.params.suffix;
ShortURL.find({ suffix: userSuffix }).then((foundUrls) => {
let urlForRedirect = foundUrls[0];
res.redirect(urlForRedirect.original_url);
});
});