URL Shortener Microservice

i do not understand what the problem is? it is not redirecting to a correct page.any help,thanks in advance.

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const dns = require('dns');
const urlparser = require('url');
const cors = require('cors');
const app = express();

// Basic Configuration
const port = process.env.PORT || 3000;
mongoose.connect(process.env.DB_URI,{ useNewUrlParser: true, useUnifiedTopology: true });
//console.log(mongoose.connection.readyState);

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

let schema = new mongoose.Schema({url: String,shorturl: Number});
let Url = mongoose.model("Url",schema);

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

// Your first API endpoint
app.post('/api/shorturl', (req, res) => {
  console.log(req.body);
const bodyurl = req.body.url;
const checkaddress = dns.lookup(urlparser.parse(bodyurl).hostname,(err,address) => {
    if(!address){
      res.json({error: 'invalid url'})
    }else{
      let shortUrl = Math.floor(Math.random() * 100000); 
      console.log(shortUrl);
      const url = new Url({url: bodyurl,shorturl: shortUrl});
      url.save((err,data) => {
        if(err) return console.log(err);
        res.json({ original_url : data.url, short_url : data.shorturl})
      })
    }
  })
 
});

app.get('/api/shorturl/:shortUrl',(req,res) => {
  let shortURL = req.params.shortUrl;
  Url.find({short_url: shortURL},(err,data) => {
    if(err){
      res.json({ error: 'invalid url' })
    }else{
      res.redirect(data.url);
    }
  })
})

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

First, set this up as a repl (like on replit.com) to make it easier for people to run and debug.

Second, you need to add logging statements to actually determine what is happening with the code. At a minimum, you need to log which route is running, what the route inputs are, and what the route outputs are at every point a response is returned to every route. You’ve got some log messages, but not nearly enough to track the state of the code as it executes.

Especially helpful would be logging (all) the data you’re using here

since I don’t think it’s what you think it is. Doing this will also give you a suitable alternative to using a small random number for the shortened URL as mongoDB provides a solution.

Further, this

returns the return value of console.log() and not the error.

finally after taking a break,i understand the problem and it was ,i used a wrong key in the find method which is short_url but from the schema as you can see it was shorturl and also i should be using findOne not find,since it returns an array.
yes of course you are right loging to the console helps a lot,thanks.and i already set up in repl.i was thinking using the id from the mongoose but it is huge and does not argue with short url i think.