URL shortener project help

URL shortener project help


Hi everyone! Hope you’re having a great Saturday :slight_smile:

I’m working through the API projects and I managed to write this URL shortener with the help of a bunch of tutorials, but I cannot get it to work. I’m obviously doing something horribly wrong :joy: but I’m still trying to wrap my head around GET and POST requests. Could someone please help me out?
This is what I’ve come up with so far:

EDIT: The error I’m getting is:

Cannot POST /api/shorturl/new

Thanks so much in advance!! I really appreciate it :slight_smile:


You’re missing a slash before the url you want to post to.


That’s not the issue. I tried both with and without slash. Slash is back now and still not working.


Alright. And that (*)?


It’s to avoid issues with double slashes. The (*) makes sure the string is accepted regardless of how it’s formatted (doesn’t assume slashes are pointing to subfolders).
But anyway, if I remove it I get the same error. Same goes for : before urlToShorten


With those parenthesis? Anyway. You’re passing the URL to be shortened as POST and you’re asking a GET parameter, right?


Ok, Sorry, I wasn’t home and couldn’t see clearly your code from my phone…

The problems are two. The first one is that you have a wrong URL (you’re handling /shorturl/ instead of /api/shorturl/ ). The second one is that you’re using pp.post('/shorturl/new/:urlToShorten(*)', ... ) and then you try to get the urlToShorten parameter, but you want to send the URL to be shortened with a form, so you must get the value inside req.body instead of req.params.

About the wildcard, I didn’t know you could use those parenthesis :slight_smile: Good to know!


Fixed it. I changed the second function to a GET and fixed some issues with the parameters and bodyParser.