Test Cases are not passing, please look at my code

Tell us what’s happening:
Describe your issue in detail here.

project is handling post method with “url” and returns a short url but the test cases are not passing.

Code

const url = []

app.post("/api/shorturl", (req, res)=>{
  dns.lookup(req.body.url.split("://")[1], (err, address)=>{
    if(err){
      return res.json({ error: 'invalid url' })
    }
    if(address){
      let num = url.length
      url[num]=req.body.url
      return res.json({
        original_url: url[num],
        short_url: num
      })
    }
  })
})

app.get("/api/shorturl/:num", (req,res)=>{
 res.redirect(url[req.params.num])
})

Your project link(s)

solution: https://short-url-basic.herokuapp.com/url/shorturl

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: URL Shortener Microservice

Link to the challenge:

Can you link to your full project code? If you have your project up on GitHub, that would be best.

Above code is the only thing I’ve added to the index.js of boilerplate below. Rest is same.

Link : GitHub - freeCodeCamp/boilerplate-project-urlshortener: A boilerplate for a freeCodeCamp project.

All the Code in index.js

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { urlencoded } = require('body-parser');
const app = express();
const dns = require("dns")

// Basic Configuration
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({extended: false}))
app.use(cors());

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.get('/api/hello', function(req, res) {
  res.json({ greeting: 'hello API' });
});

const url = []

app.post("/api/shorturl", (req, res)=>{
  dns.lookup(req.body.url.split("://")[1], (err, address)=>{
    if(err){
      return res.json({ error: 'invalid url' })
    }
    if(address){
      let num = url.length
      url[num]=req.body.url
      return res.json({
        original_url: url[num],
        short_url: num
      })
    }
  })
})

app.get("/api/shorturl/:num", (req,res)=>{
 res.redirect(url[req.params.num])
})

app.listen(port, function() {
  console.log(`Listening on port ${port}`);
});

First you need to resolve your CORS issue. Currently, when https://freeCodeCamp.org attempts to post to your app, it can not because of your current settings of the cors module. Review the Advanced Node challenges for help with that.

Also, the dns.lookup method expects just a domain name and nothing extra (like querystring. You need to console.log what you are attempting to pass to it and you will see it is not just a domain name. Also, your app let’s you shorten invalid urls like http://example.com

Also, even though the tests can not check for it, your app is poorly designed because every time you restart it, you loose all the urls that have been submitted to it. You need implement persistent data storage of the urls, so if the app restarts (due to changing your code), any url already shortened before you last restarted it, will still be able to redirect correctly. You learned how to use mongo/mongoose, so I suggest making use of a database to store the urls in.