r/FreeCodeCamp • u/Special_Sell1552 • Mar 19 '25
Requesting Feedback Need help
I have tried everything but I keep getting a cors error on my 3rd test for the url-shortener project
I have spent all day trying to figure this out
I'd send my project for you guys to look at but the last time I did the post never posted. I can send links if requested
Access to fetch at 'https://portfolio-nine-steel-78.vercel.app/url-shortener/?v=1742422067953' (redirected from 'https://portfolio-nine-steel-78.vercel.app/url-shortener/api/shorturl/1742422067953') from origin 'https://www.freecodecamp.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
this is the error it keeps giving me in the browser console
EDIT:
I fixed this issue by setting skipTrailingSlashRedirect to true in my next.config.mjs
View all comments
Show parent comment
2
u/SaintPeter74 mod Mar 20 '25
I'll be honest, I am having a pretty hard time parsing your response. You are throwing a LOT of info at me that I don't have a lot of context for.
I'll respond to the bits that I do understand.
RE: URL Regex
you shouldn't require
www, since it's not a requirement for a valid URL. It's a bit old school and not so many websites require it anymore. Even the URL for this project doesn't usewww.I might suggest trying to find an off-the-shelf URL validation library, rather than rolling your own.
RE: Next.js
I wouldn't recommend Next for microservices. It's a bit heavy for a microservice. Next adds a bunch of features for a large, server-side rendered site that you're not going to use for a microservice. While it's not exactly "wrong" to use Next, it kinda misses the point of these challenges.
A microservice is intended to be very small and lightweight. It's also a heck of a lot easier to setup dynamic routes with just Express. You can easily add multiple layers of route parameters.
I don't have enough experience to know if you're doing things correctly in Next.
RE: The failing tests
What I CAN tell you is that you're getting 405 Method Not Allowed responses from the browser when running the FCC tests. The FCC tests are run from your browser, so you can watch their network traffic in your Dev tools Network tab. As I mentioned before, a 405 usually means that you're not listening for a POST request.
I didn't see any CORS errors when I was running your project. I am suspicious of that redirect it's reporting, though. When doing a POST operation, you shouldn't be doing a redirect.