My Debut as a Backend Developer: How I deployed a URL Shortener API
Israel Rotimi

Israel Rotimi @israelrotimi

About: I'm an expert technical writer and web developer that takes passion in simplifying complex concepts, solving problems and and using tech to help humanity. I use the MERN stack and TypeScript.

Location:
Abuja, Nigeria
Joined:
Dec 4, 2023

My Debut as a Backend Developer: How I deployed a URL Shortener API

Publish Date: Jun 19
0 0

Hey there, nice to talk about my dev journey again.
I've been a frontend developer for majority of my career, even though I learned Node.js early on I had not fully dived in and deployed a fully functional project end-to-end-- until now.
Here's I explain what it was like, what I faced and how I built it.

The challenge

I searched online for backend projects to build and took the the basic overview they gave as a starting point.
I was to build a URL Shortener service where a long URL can be sent to the server and a shortened URL returned. I was to create a catch-all route that would intercept the requests from a shortened URL search for it's matching long URl and redirect to it. Simple Right? Not really.

My solution

So, I Started building. I set up basic app stucture, then I stopped taught long and hard and came up with this solution.

I'll only need one database model or Schema, since there'll be no auth in the app.
I would

  • retrieve the long URL from the request
  • store it in the DB Schema
  • generate a unique ID for the URL
  • And store that simultaneously with the longer URL

I figured that generating a unique ID was a simpler solution I could implement rather than actually performing operations on the original string as that would be entering the realm of Cryptography.

I used AI to come up with a solution to generate a unique string and it came up with a simple solution that would work just fine until my app had many users and was used many times a day.

 shortenedStringId: {
        type: String,
        unique: true,
        // This implementation can generate approx. 56.8 billion unique strings.
        // That'll be okay for now, but if you want to use in production
        // Consider installing a package like nanoid to generate unique strings
        // or consider implementing a more robust solution.
        default: function() {
            const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            let result = '';
            for (let i = 0; i < 6; i++) {
                result += chars.charAt(Math.floor(Math.random() * chars.length));
            }
            return result;
        }
    },
Enter fullscreen mode Exit fullscreen mode

Challenges I faced

I faced some real challenges when I tested what I built. But thanks to a robust error management system I put in place, an error middleware, I could pinpoint were the error was coming from.
I fixed a few import errors but couldn't pinpoint one bug.
Finally I placed it in Chatgpt and asked it if it was valid.
I turned out to be a subtle error were I forgot to destructure the URL from req.params

//My previous code
const url = req.body;
// LLM corrected
const { url } = req.body;
Enter fullscreen mode Exit fullscreen mode

This emphasizes how a fresh pair of eyes sometimes could spot what you didn't.

Next Steps

Building this App taught me debugging as a backend developer and increased my confidence in handling problems.
In returning the shortened URL, I wasn't sure of the live URL so I used req.baseUrl but that didn't work as expected.

res.status(201)
    .json({
      originalURL: newUrl.targetURL,
      shortURL: `${req.baseUrl}/${newUrl.shortenedStringId}`,
    })
Enter fullscreen mode Exit fullscreen mode

I don't know a better solution, if you know a better solution, please mention in the comments.

Check out the GitHub repo at https://github.com/israelrotimi/url-shortener
And please leave a star ⭐

It's deployed on render https://url-shortener-api-o0n2.onrender.com/

Comments 0 total

    Add comment