Back End Development and APIs Projects - URL Shortener Microservice

this are my code i don’t know if it’s my replit bothering i don’t know what to do right know

const originalUrls = [];
const shortUrls = [];

app.post("/api/shorturl/new", (req, res) => {
  const url = req.body.url
  const foundIndex = originalUrls.indexOf(url);
  console.log("bonjour", url);
  if (!url.includes("https://") && !url.includes("http://")) {
    return res.json({ error: "Invalid url" })
  }

  if (foundIndex < 0) {
    originalUrls.push(url)
    shortUrls.push(shortUrls.length)

    return res.json({
      original_url: url,
      short_url: shortUrls.length - 0
    });
  };

  return res.json({
    original_url: url,
    short_url: shortUrls[foundIndex]
  });
});

app.get("/api/shorturl/:shorturl", (req, res) => {
  const shorturl = parseInt(req.params.shorturl)
  const foundIndex = shortUrls.indexOf(shorturl);

  if (foundIndex < 0) {
    return res.json({
      "error": "No short URL found for the given input"
    });
  }
  res.redirect(originalUrls[foundIndex])
});

here is the link

https://replit.com/@bowak/boilerplate-project-urlshortener-2#index.js

type or paste code here

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.