Back End Development and APIs Projects - URL Shortener Microservice, have completed the task but third check does not pass

Tell us what’s happening:

I’ve completed the tasks, it’s working properly on Replit, but the third check does not pass. Any suggestion?

Your project link(s)

solution: https://boilerplate-project-urlshortener.gcosta815.repl.co/

require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const dns = require("dns");
const validUrl = require("valid-url");
const mongoose = require("mongoose");

const port = process.env.PORT || 3000;

app.use(cors());
app.use("/public", express.static(`${process.cwd()}/public`));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

app.get("/", function (req, res) {
  res.sendFile(process.cwd() + "/views/index.html");
});

const urlSchema = new mongoose.Schema({
  original: { type: String, required: true },
  short: { type: String, required: true },
});

const Url = mongoose.model("Url", urlSchema);

app.post("/api/shorturl", (req, res) => {
  const fullUrl = req.body.url;

  if (!validUrl.isWebUri(fullUrl)) {
    res.json({ error: "Invalid URL" });
  } else {
    const urlObject = new URL(fullUrl);
    const hostname = urlObject.hostname;

    dns.lookup(hostname, (err) => {
      if (err) {
        res.json({ error: "Invalid Hostname" });
      } else {
        const newUrl = new Url({
          original: req.body.url,
          short: encode(req.body.url),
        });
        newUrl
          .save()
          .then(() => {
            console.log("Url saved successfully");
          })
          .catch((error) => {
            console.error("Error saving Url:", error);
          });

        res.json({
          original_url: req.body.url,
          short_url: encode(req.body.url),
        });
      }
    });
  }
});

app.get("/api/shorturl/:short_url", (req, res) => {
  Url.find({ short: req.params.short_url }, (err, url) => {
    if (err) {
      console.log("Error finding Url: ", err);
    } else {
      if (url.length > 0) {
        res.redirect(url[0].original);
      } else {
        res.json({ error: "No short URL found for the given input" });
      }
    }
  });
});

app.listen(port, function () {
  console.log(`Listening on port ${port}`);
});

function encode(input) {
  return Buffer.from(input).toString("base64").slice(10, 20);
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Back End Development and APIs Projects - URL Shortener Microservice

Log out the value you are saving to short_url, it is not unique.

console.log(Buffer.from('https://example.com/?v=1702317419236').toString("base64").slice(10, 20)) // 9leGFtcGxl
console.log(Buffer.from('https://example.com/?v=somethingnew').toString("base64").slice(10, 20)) // 9leGFtcGxl

Thanks, I got it. What kind of hashing are we supposed to use instead?

That is really up to you. It doesn’t have to be “reversible” it just has to be something unique and I guess not too obnoxious to look at considering it is what the user gets back to use as the short URL.