URL Shortener Microservice Challenge: Can't pass any tests

Tell us what’s happening:
Hi, my project can do the stuff the test asks for but can’t pass anything other than the Unique URL test.

Your project link(s)

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: URL Shortener Microservice

Link to the challenge:

Hello there,

I suggest you try move this line to the end of your script:

app.listen(port, function () {
  console.log('Node.js listening ...');
});

Otherwise, do you not see any errors in the app console?

1 Like

Also you need to handle posts to ‘/api/shorturl’.

app.post(’/api/shorturl/new’…

not to ‘/api/shorturl/new’.

2 Likes

No it runs without errors for me :confused:

that broke the parser, any URL is now invalid.

The Invalid URL test is passing now though :smile:

That would not break the parser, try to set extended to true.

And you could just use it like this before the routes:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

Also you now can use it with express instead of bodyParser after express version 4.16.0

1 Like

I set extended to true, same response. By “before the routes” did you mean above app.post? I’m not sure what routes are here.

app.use(’/public’, express.static(process.cwd() + ‘/public’));

You can put it before or after this line.

Remove bodyparser from your routes as a middleware.

1 Like

I also replaced

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

with their Express counterparts:

app.use(express.json());
app.use(express.urlencoded());

I now see a warning in the console,
body-parser deprecated undefined extended: provide extended option server.js:56:17

I really appreciate your time, thanks for helping me.

1 Like

app.use(express.urlencoded());

set extended to true here.

I’ve remeoved body parser, and placed the app.use(express) lines above the routes like you said. Is extended set to true as:
app.use(express.urlencoded({extended: true}));

Doing so made no change.

Maybe the issue is with how the RegEx finds a match with the request.body/inputURL, but the application was to tell the difference between valid and invalid urls before changing how posts were handled

new RegExp(/https?://(www.)?[-a-zA-Z0-9@:%.+~#=]{1,256}.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%+.~#?&//=])|[-a-zA-Z0-9@:%.+~#=]{1,256}.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%+.~#?&//=])/, “gi”)

This is from stackoverflow.

Your handling seems fine try to change the regex, a few minutes ago your all tests were passing but url validation.

Let me know if you pass all the tests. You are doing great.

1 Like

I got it :smile: Had to change the URL validation to use test() for the reg ex stuff.
Thanks again for your time!

1 Like

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