Not Passing URL Shortener Microservice Project Tests

I dont know why test are not passing. All is working!

I send you my code:

server.js

require('dotenv').config();
const express = require('express');
require("./database.js");
const cors = require('cors');
const app = express();
const dns= require("dns");
const Web= require("./model.js");
// Basic Configuration
const port = process.env.PORT || 3000;

//Middlewares
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.use('/public', express.static(`${process.cwd()}/public`));

//Routes
app.get('/', function(req, res) {
  res.sendFile(process.cwd() + '/views/index.html');
});


app.post("/api/shorturl",  (req,res)=>{
  const {url}=req.body;
  const urlBasic=url.replace(/^https:\/\//,"");

  dns.lookup(urlBasic, async(err,addresses, family)=>{
    if(err){
      return res.json({error: 'invalid url'} )
    }  
  
  
  const busqueda=await Web.find().sort({short:-1})
    if (busqueda.length==0){
      const newWeb= new Web ({original:url, short:1} );
      const saved=await newWeb.save()
      return res.json({ original_url : saved.original, short_url : saved.short})
    }

  let newId=busqueda[0].short +1;
  const newWeb= new Web ({original:url, short:newId} );
  const grabado=await newWeb.save()

  return res.json({ original_url : grabado.original, short_url : grabado.short})

  })
})

app.get("/api/shorturl/:short", async (req,res)=>{
  const {short}=req.params;
  
  try{
    const found =await Web.find({short:short});
    var re = new RegExp("^(http|https)://", "i");
    const web= found[0].original;
    
    if(re.test(web)){
      return res.redirect(301,web);
    }
    
    else{
      const nuevaweb= "http://" + web;
      return res.redirect(301,nuevaweb)
    }
  }
  catch(err){
    res.json({error:"invalid URL"})
  }

})
// Your first API endpoint
app.get('/api/hello', function(req, res) {
  res.json({ greeting: 'hello API' });
});

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

this is my replit if you prefer
https://replit.com/@jesuscano80/boilerplate-project-urlshortener

Thank you friends!

Thanks!

Have you tried it with longer urls? For example address of the project page on freeCodeCamp or address of the project.

Hello there,

Note: dns.lookup expects the domain name only for the first arguement.

Hope this helps

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