URL shortener challenge

Hi everyone, so the problem is that I don’t know why i can’t pass this challenge.

i tested it many times and it looks good for me!

do i need to store the data in MongoDB?

IDK, Please if you have some explanation why this is not passing, I’ll appreciate that

My code so far

// make an array of original urls
const shortUrls = ["https://freeCodeCamp.org"]

app.post("/api/shorturl",(req,res) => {
  const {url} = req.body
  try {
    var expression = new RegExp(/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi);

    if(!url.match(expression)) throw new Error('Whoops!')
  return res.json({original_url : url,shorturl:shortUrls.length})
  }catch(err) {
    return res.json({ error: 'invalid url' })

app.get('/api/shorturl/:data',(req,res)=> {
  const {data} = req.params
  // check if the shorturl passed by the user is exist or not on the array
  if(shortUrls.length >= +data)
    return res.redirect(shortUrls[data - 1])
  return res.json({ error: 'No short URL found for the given input' })

Challenge: URL Shortener Microservice

Link to the challenge:

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