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 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
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
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 Good to know!
What did you do to fix the bodyParser?
My issue is that the req.body seems to always be empty, which could mean that either the actual post request data isn’t being written somehow or my bodyParser is wrong. Right now I’m using the bodyParser from the tutorials: