CORS issue with FCC tests on microservices url shortener

Tell us what’s happening:
Describe your issue in detail here.
When I use curl or the repl.it web interface on my current project, I see the output I expect from my code:

sean@work:~> curl -d "url=www.freecodecamp.com" -X POST https://boilerplate-project-urlshortener.seandoe.repl.co/api/shorturl
{"original_url":"www.freecodecamp.com","short_url":"placeholder"}

However, when I run the FreeCodeCamp tests, the associated test fails. Specifically, this test:
" You can POST a URL to /api/shorturl and get a JSON response with original_url and short_url properties. Here’s an example: { original_url : 'https://freeCodeCamp.org', short_url : 1}"

You’ll see in my code that I’ve explicitly enabled CORS with app.use(cors())

However, when I use the developer tools during the FreeCodeCamp test runs, I receive multiple CORS errors:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://boilerplate-project-urlshortener.seandoe.repl.co/api/shorturl. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘*, *’).

I don’t expect my project to pass all tests yet, as I haven’t implemented URL shortening- however, I can’t tell if I’m doing something fundamentally very wrong, or if I’m having testing issues because of some kind of interaction between replit and FCC. I log the URLs that FCC is passing, and I don’t understand their contents, it looks like the project is calling itself, however I checked the request body and this is the literal only url present:

url: 'https://boilerplate-project-urlshortener.seandoe.repl.co/?v=1641749593580'

Furthermore, I’m able to click the link and it DOES resolve.

Thank you in advance for your time- and if you need any additional information I’m happy to provide it.

Your project link(s)

solution: https://replit.com/@seandoe/boilerplate-project-urlshortener

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: URL Shortener Microservice

Link to the challenge:

I’m not sure about the error; I just forked and tested the project without CORS errors and the setup looks fine. It was possibly a transient problem somewhere in the system. Sometimes though, requests will timeout and the browser console will get confused and flag it as a CORS error incorrectly.

The tests do indeed use your URL with some parameters for testing. Your urlResolves function is not working properly since you’re giving dns.lookup() a URL with parameters and not a hostname. Consult the documentation for details.

1 Like

Thank you for taking the time. I read the entirety of the documentation, and wasn’t able to determine what explicitly set the URL apart from the hostname in the context of a lookup- however, it became clear to me what you mean after some testing:

const dns = require('dns');

const resolveUrl = (providedUrl, callback) => {
        dns.lookup(providedUrl, (err, address, family) => {
                let result = 'placeholder';
                if(err){
                        console.log(`error in resolution of ${providedUrl}`);
                        result = 'Lookup failed';
                        callback(err, result);
                        } else {
                        console.log(`resolution success for ${address}`);
                        result = 'Lookup succeeded';
                        callback(null, result);
                        }
                        });
                }

        const lookupResult = (err, args) => {
                console.log(args);
                }

resolveUrl('www.freecodecamp.com', lookupResult);  //works
resolveUrl('boilerplate-project-urlshortener.seandoe.repl.co', lookupResult); //works

resolveUrl('https://boilerplate-project-urlshortener.seandoe.repl.co/?v=1641767342205', lookupResult); //doesn't work

Then, I realized that the docs themselves mention utilizing the OS utilities, so I attempted a dig to better understand what was going on:

➤ dig +short boilerplat-project-urlshortnere.seandoe.repl.co                                                                                                                                                                                                                                                         14:45:37
35.186.245.55
sean@work:~/fcc
➤ dig +short https://boilerplate-project-urlshortener.seandoe.repl.co/?v=1641767342205                                                                                                                                                                                                                               14:45:51
fish: No matches for wildcard “https://boilerplate-project-urlshortener.seandoe.repl.co/?v=1641767342205”. See `help expand`.
dig +short https://boilerplate-project-urlshortener.seandoe.repl.co/?v=1641767342205

Thank you so much @jeremy.a.gray for taking the time and helping me, I felt super stuck on this and you’re saving me a lot of time and despair.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.