Problem to validate on URL Shortener Microservice challenge

I’m pretty sure I did everything right and I’m not getting the expected result. Can anybody help me?

doing the POS T and GET with POSTMAN is working, but when I try to validate on the site it gives me an error.

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
const dns = require("dns")

// Basic Configuration
const port = process.env.PORT || 3000;

var dataBase = []

app.use(cors());

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

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

app.use(express.json())

function middleware (req, res, next){
  const url = req.body.url.slice(8)
  
  dns.lookup(url, (err, address, family) =>{


    if(err) return res.json({error: 'invalid url'})

    console.log(req.method)
    return next();

  })  
}

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' });
}); */


app.post("/api/shorturl",middleware, (req, res ) => {

    const url = req.body.url

    const randomNumber = Math.round((Math.random() * 100))

    dataBase.push({
      original_url:url,
      short_url:randomNumber
    })

    return res.json(dataBase[dataBase.length - 1])

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

    let short = req.params.shorturl

    let result = dataBase.find( el => new String(el.short_url) == short)
  
    if(result != undefined){
      return res.redirect(result.original_url)
    }else{
      return res.json({error: 'invalid url'})
      
    }
});

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

Your project link(s)

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

Your browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

Remove dns.lookup and just use regex to check url validity.

1 Like
 function validarURL(url){  
      if(/((https?:\/\/)|(ftp:\/\/)|(^))([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+([a-zA-Z]{2,9})(:\d{1,4})?([-\w\/#~:.?+=&%@~]*)/.test(url)){  
           alert('Formato correto !');  
      } else {  
           alert('Formato incorreto !');  
      }  
 }

Thank you…