Code works but failing test - URL Shortener Microservice

Other than the code below, all from gh repo is unchanged:

// ...

const bodyParser = require('body-parser')
const dns = require('dns')

// ...

app.use(bodyParser.urlencoded({ extended: false }))

let urls = []

// Verify URL and to array and return it with a short url (int)'/api/shorturl', (req, res) => {
    const url = req.body.url
    const host = url.replace(/^.*?:\/\//, '')
    dns.lookup(host, (err) => {
        if (err) return res.json({ error: 'invalid url' })
            original_url: url,
            short_url: urls.length

// Access the URL via it's index in array
app.get('/api/shorturl/:n', (req, res) => {
    res.redirect(urls[req.params.n - 1])

// ...

The code works in the I can enter and get the response {"original_url":"","short_url":1}. I can then go to the address bar and type http://localhost:3000/api/shorturl/1 and am redirected to freeCodeCamp’s website. I can then go back and type and get the response {"original_url":"","short_url":2}. In the address bar I can enter http://localhost:3000/api/shorturl/2 and am taken to Google’s website. Finally, I can enter ftp:/john-doe.invalidTLD and get the response {"error":"invalid url"}. This is the extent of the testing. However, it fails both tests two and three:

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 : '', short_url : 1}

When you visit /api/shorturl/<short_url> , you will be redirected to the original URL.

In this case the test seems wrong because I can POST a url to /api/shorturl and do get a JSON response with original_url and short_url properties; likewise, I am redirected to the original URL when I visit /api/shorturl/<short_url>.

Hello. Been messing around with your code, and while I’m not sure exactly what’s causing it to fail, here are a few things I noticed where your code differs from mine, which passes all tests.

  1. You allow not including the http:// in your URLs. It works to create the shortcut, but when you attempt to use the shortcut I usually get a browser redirect failure, because it doesn’t include the http:// in the redirect. In my code, the “http://” part was required or you’d get invalid URL.

  2. You don’t check to see if your shortcut is valid when you enter a URL shortcut, instead it attempts to redirect with a null value, causing a browser redirect error.

  3. You apply the middleware to all requests, not just POST, but I changed that in the code and yours still failed, so doubt thats an issue.

  4. I store URLs in a DICT instead of an array, using the shortcut as the key, but can’t image why that would matter.

So not sure if the tests maybe attempt to enter a URL without https:// to see if you’re strictly adhearing to that format or maybe tries intentionally entering an incorrect shortcut number, but might be something to check.

  1. You are not providing the dns.lookup method with the correct thing. It has to be the hostname only. If you submit and look at the response using the network tab you can see what you are returning.

You can use the URL() constructor.

new URL('').hostname
// ''
  1. I would suggest using an actual DB for this.