Url shortener test 3 error

i only get error when i run the test when i try “https://www.google.com” its working fine
can anyone suggest where my code is wrong
this is my code:

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
const mongoose = require('mongoose');
var bodyParser = require("body-parser");

mongoose.connect(process.env['MONGO_URI'], { useNewUrlParser: true, useUnifiedTopology: true });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Basic Configuration
const port = process.env.PORT || 3000;

app.use(cors());

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

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

let url_schem = new mongoose.Schema({
  url: { type: String, required: true },
  short_url: { type: Number, default: 0 }
});

var urlSchema = mongoose.model('urlSchema', url_schem);
  
// Your first API endpoint
app.post('/api/shorturl', function(req,res) {
  var url_info = req.body.url;
  var short_value = url_info.length
  var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  
  if(regexp.test(url_info) == false){
    res.json({error: 'invalid url'});
  };
  
  var new_insert = new urlSchema({url: url_info, short_url: short_value})

  new_insert.save(function (err,data){
    if(err) return res.json({error: err })
    else{
      res.json({original_url: url_info,short_url: short_value});
    }
  })

});

app.get('/api/shorturl/:id', function(req,res){
  urlSchema.findOne({short_url: req.params.id}, function (err,data){
    if (data != null){
      return res.redirect(302,data.url)
    }else{
      return res.json({error: "invalid url"})
    }
  });
});

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

this is my error:

Add a return to all the res calls that have conditions (or just to all of them).

this the link to my replit https://boilerplate-project-urlshortener.kwstasmour.repl.co

i tried that and now i dont pass the test but i get no error

or do you want this link boilerplate-project-urlshortener - Replit

It is because you are using url_info.length as the short_value. The length is not a very unique value.

that was it thank you changed it to math.random and i passed

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