Url shortener without database?

So, I finished this project and i think, it works as it should. The problem is that I can’t get the tests to pass. I am saving urls directly in server file and not in a database - could that be a problem?

let urls = ["https://www.gmail.com"];

//in post callback function
urls.indexOf(url)  <  0  ?  urls.push(url)  :  null;
res.json({
original_url : url, 
short_url : urls.indexOf(url)
});

Challenge: URL Shortener Microservice

Link to the challenge:

Reading through the test cases using a database shouldn’t matter.

When you say that you’re saving urls directly in a server file can you elaborate a bit?

If you’re storing them in an array that won’t work. But if you’re reading and writing to a static file somewhere (which I don’t see you doing in your code snippet) you should be able to pass the tests.

Yeah, I’m saving them in an array, so that must be it:

let urls = ["https://www.gmail.com"]

app.use(express.json()) 
app.use(express.urlencoded({ extended: true })) 

app.post("/api/shorturl/new", (req, res) => {
  let url = req.body.url
  console.log(url.split(/https*:\/\/www./)[0])
  dns.lookup(url.split(/https*:\/\/www./)[0], (err, address)=>{
    if(err) {
      res.json({ error: 'invalid url' })
    } else {
      urls.indexOf(url) < 0 ? urls.push(url) : null;
      res.json({
        original_url : url, 
        short_url : urls.indexOf(url)})
    }
  })
})

app.get("/api/shorturl/:url", (req, res) => {
  res.redirect(urls[req.params.url])
})

Could you please explain why this is a problem?

Thank you for the answer!

Oh I’m so sorry I completely missed the notification on this. I will give a detailed answer tomorrow morning I promise

So what’s happening from what I can tell is that when you call api/shorturl/new you check to see if it exists and if it doesn’t you push the new url onto urls which is your array.

But you never store that value anywhere permanent. The array only lives in memory and as soon as the script is done executing that data is gone. The next request that comes in will run the script again and you’ll recreate your urls variable and initialize it to “https://www.gmail.com”.

Every time you send a request to /api/shorturl/new the line: let urls = ["https://www.gmail.com"] is ran.

This is why sending a request to /api/shorturl/:url will never work because any url you try to add will never persist between requests.

This is where most people would use a database, but to answer your original question, no the project doesn’t require a database.

It doesn’t matter how you store the data so long as it lives somewhere that persists between requests to your server. You could write the data to a raw text file, or a json file if you want. But it needs to be stored somewhere that persists after the script is done executing and your script needs to be modified to read the data from wherever that storage is whether it’s a plain text file or a database.

Hope that helps to clarify all this!

Oh, thank you so much, now I understand it

1 Like