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.

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('/public', express.static(`${process.cwd()}/public`));



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'})

    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' });
}); */"/api/shorturl",middleware, (req, res ) => {

    const url = req.body.url

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


    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)
      return res.json({error: 'invalid url'})

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

Your project link(s)


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){  
           alert('Formato correto !');  
      } else {  
           alert('Formato incorreto !');  

Thank you…

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