My URLShortener App works as expected, but wont pass any test

Can you help me out finding the issue? This is the link, and it is running. As described on the tittle, I think my app isworking as expected, but I can’t pass any test from freecodecamp, expect the first one that says “You should provide your own project, not the example URL.”

https://repl.it/@duke33/boilerplate-project-urlshortener-2#server.js

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: URL Shortener Microservice

Link to the challenge:

Hello there,

Some things I notice:

  1. The first argument for dns.lookup must only be the domain name:
const REPLACE_REGEX = /^https?:\/\//i;
    console.log("ANDA POR FAVOR" + req.body.urlToBeShortened)
    req.body.urlToBeShortened = req.body.urlToBeShortened.replace(REPLACE_REGEX, '');
    //Check if it is a valid URL
    dns.lookup(req.body.urlToBeShortened, function(err, addresses) {
  1. I see this error, when I try to submit your code:
POST https://boilerplate-project-urlshortener-2--duke33.repl.co/api/shorturl/new/ 500
eval @ VM50:5
frame-runner.js:98 Error: 500 
    at eval (eval at <anonymous>

This shows there is an issue with the app crashing. I would expect your Replit terminal to show more errors.

  1. I highly recommend you make use of a .env file to store your DB URI. Anyone can find and take your URI, now, and abuse your database account - not so much a big deal on a free account, but, if you one-day forgot, and connected your credit/debit card to it, then you could be charged a ridiculous amount of money for someone else using it. Also, do not even get me started on the fact that someone could use your DB for malicious/illegal purposes, and you would get the blame.

So, can you see any errors in the Replit console?

Gosh, thanks for the headups! I’ll fi it and I’ll come back

Well, the thing is that I don’t see any error output when I try the app with, for instance, https://www.freecodecamp.org as the url to be shortened!

The regex will trim it, so, DNS lookup would get www.freecodecamp.org, and the method handles it, I don’t know if I should trim the subdomain, but the thing is that it works, at least on my tests!

This is the output error that I get when run fcc tests:


node v12.16.1
TypeError: Cannot read property 'replace' of undefined
    at create_url_record_on_DB (/home/runner/boilerplate-project-urlshortener-2/server.js:50:89)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:275:10)
    at cors (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:188:7)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:224:17
    at originCallback (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:214:15)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:219:13
    at optionsCallback (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:199:9)
    at corsMiddleware (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:204:7)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:317:13)
TypeError: Cannot read property 'replace' of undefined
    at create_url_record_on_DB (/home/runner/boilerplate-project-urlshortener-2/server.js:50:89)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:275:10)
    at cors (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:188:7)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:224:17
    at originCallback (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:214:15)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:219:13
    at optionsCallback (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:199:9)
    at corsMiddleware (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:204:7)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:317:13)
TypeError: Cannot read property 'replace' of undefined
    at create_url_record_on_DB (/home/runner/boilerplate-project-urlshortener-2/server.js:50:89)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:275:10)
    at cors (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:188:7)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:224:17
    at originCallback (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:214:15)
    at /home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:219:13
    at optionsCallback (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:199:9)
    at corsMiddleware (/home/runner/boilerplate-project-urlshortener-2/node_modules/cors/lib/index.js:204:7)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/boilerplate-project-urlshortener-2/node_modules/express/lib/router/index.js:317:13)

You are doing very few tests.

Try these:

  • https://freecodecamp.org/learn/test-page
  • ftp://some-otherpage.com

See what happens. Hope this helps

1 Like

You are right, I’ll take a look at it and try to fix it. Thanks, I do really apreciate your help!!

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