Needed help starting with the url shortner microservice

Hi i was starting with the 3rd project for the api and microservices section…

i was reading up about how to create custom url shortener using node express and mongoose…

so i really have no idea on how to create… can someone please give some guides to follow or tips to get me started.

edit: i didnt want to see a code video or use node-url-shortener… wanted to try build something by myself before seeing the code along videos

Well, to start with, what would you use as the schema for mongoose?

Once you have a schema, what are your routes? You’ll probably need two: one for POST requests (to add a new route), and one for GET (to fetch a route with a given ID).

i think 2 key value pairs

originalurl and a shortened version both would be strings i suppose

is it necessary to be using a database?
can it be done simply by using node and express?

So, in that case, how are you storing the records of what shortened URL points to which actual URL?

i was thinking i can take the url from the post…
do some changes in the app.post… then send it to a app.get with the shortened url as a path…
not very clear at this point but i guess using a database might be the less messier way to go then

Basically, you want to POST with a true URL, (validate that as an actual URL), and then save that to the database (or to a flat file, or to SOMETHING). Then, return the ID from that save as the shortenedURL. Handle errors etc.

Then, you want to GET with that shortened URL, which should find the record from the database with that id, and return the full URL.

EDIT: Rather than using the actual ID, you would want to create something like an index property in that schema. The index would be the total number of the records thus far, thus setting the zero-based index to the next open number.

2 Likes

cool thanks for improving/creating the mental picture… i was a bit unclear on why we are using mongodb here… :smiley:

Hope it helps. I did have fun with this challenge. The most difficult part for me was with the DNS package.

The thing is, as you learn the pattern, it’s much the same with ANY mongoose interaction. CRUD is dismayingly simple, when you wrap your head around it.

definitely… it was just a sudden step up from the first two ones… this one does feel like a real project though

i have just managed to set up a schema which has originalUrl and a shortUrl… dont know but if it connects properly to the mlab link. Any way to check if it works or not the database at this stage currently?

also have got a post request working which i can then use dns.lookup to check url and then pass to database if it passes through dns.lookup function. will continue again tomorrow …

thanks for the initial push/guide :smiley:

edit: my early files link

i got it finished :smiley: thanks for the easy breakdown…

i was thinking if i had to shorten the initial part of the url too… can i apply the same method creating a new schema and two routes…

1 Like

Absolutely. Should work fine

im not sure if that will be possible though… i want to shorten the path before the rootpath…

like eg. my current shortened url will be something like
https://url-sh0rtn3r.glitch.me/api/shorturl/9575

i was thinking i can somehow make it like

https://xyz/api/shorturl/9575

so i can perform get requests for like api/shorturl/:newurl

but unsure for how to access the initial part of the route…

take a look at window.location.host or more broadly window.location on MDN.

Possibly window.location.hostname, unless you need to include any port number as part of the host URL (for example, my dev server uses port 3030 for my node server, if I wanted to include THAT, I’d use window.location.host as that does include the port number).

thanks ill try that… i was thinking mostly changing the location to required url would be enough to rerun location.address, hopefully.

What i was wondering was like in a get request the path route- it always begins with a “/” which specifies to the root path. Is there a way to change that to specify to the initial part… like for me it would be https://url-sh0rtn3r.glitch.me. or if its run locally can the path route access localhost:3000/
before the root route

Given a posted url, i would perhaps use a regex with a lookbehind and lookahead, and simply replace the capture group. You want the lookbehind to be your protocol, http:// or https:// or whatever, and your lookahead to the first slash after.

Crap. Your shortcut will likely be http, but urls can be all sorts: http, https, ftp…

i mean like when i do a get request it is like

app.get( “path”, function(to get executed when the path is hit))

so the path is always starting from " / " then comes the lookahead. can i type something in there so that when i type the lookbehind in the url bar the request-function gets called

ok i think i got what u mean… so using a regex for like lookbehind/lookahead to change both will it work for if i put it in any request?

1 Like