Back-End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:

I can’t validate this test:
“3. When you visit /api/shorturl/<short_url>, you will be redirected to the original URL.”

I’ve tried everything and haven’t been able to validate it.

My code:

require('dotenv').config();

const express = require('express');

const cors = require('cors');

const app = express();

const { MongoClient } = require('mongodb');

const dns = require('dns')

const urlparser = require('url')




const client = new MongoClient(process.env.DB_URL)

const db = client.db("urlshortner")

const urls = db.collection("urls")




// Basic Configuration

const port = process.env.PORT || 3000;




app.use(cors());

app.use(express.json())

app.use(express.urlencoded({extended: true}))




app.use('/public', express.static(`${process.cwd()}/public`));




app.get('/', function(req, res) {

  res.sendFile(process.cwd() + '/views/index.html');

});




// Your first API endpoint

app.post('/api/shorturl', function(req, res) {

  

  console.log(req.body)

  const url = req.body.url

  const dnslookup = dns.lookup(urlparser.parse(url).hostname, async (err, address) => {

    if (!address){

      res.json({error: "Invalid URL"})

    } else {




      const urlCount = await urls.countDocuments({})

      const urlDoc = {

        url,

        short_url: urlCount

      }




      const result = await urls.insertOne(urlDoc)

      console.log(result);

      res.json({ original_url: url, short_url: urlCount })

      

    }

  })

});




app.get("/api/shorturl/:short_url", async (req, res) => {

  const shorturl = req.params.short_url

  const urlDoc = await urls.findOne({ short_url: +shorturl })

  res.redirect(urlDoc.url)

})




app.listen(port, function() {

  console.log(`Listening on port ${port}`);

});

Help pls

Your browser information:

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

Challenge Information:

Back-End Development and APIs Projects - URL Shortener Microservice

hi and welcome to the forum,

I think this exercise is no longer being maintained so may have a bug on the fCC side.
Review the comments made here and see if you’re in the same boat: