HELP! Cannot pass my URL Shortener although I believe it to be functioning correctly

Tell us what’s happening:*
I’ve literally written 3 different ways to achieve the outcome required however I’m still not passing the <short_url> redirect.

Your code so far

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

 

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

//mongodb
mongoose.connect("mongodb+srv://kevsri:<mypassword>@cluster0.wd1gv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true });

console.log(mongoose.connection.readyState);

app.use(cors());

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

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

//routes >>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

const { Schema } = mongoose;

//Schema and Model creation to store the URL to db
var ShortUrl = mongoose.model('ShortUrl', new Schema({
  short_url: String,
  original_url: String,
  suffix: Number
}));


// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

let suffix = 1;
console.log(suffix, "<= this is the suffix")



app.post('/api/shorturl', function (req, res) {
  
  let clientReqUrl = req.body.url 
  let short_id = suffix;
  console.log(short_id, "<= This will be the short_id")
  console.log(req.body.url, "<= This is the client Requested Url");

  // create a Model from the Schema and call it newUrl
  
  let newUrlModel = new ShortUrl({
    short_url: "https://boilerplate-project-urlshortener-3.kevinsrithevan.repl.co/api/shorturl/" + short_id,
    original_url: clientReqUrl,
    suffix: short_id
  })
  res.json({
    'saved' : true,
    'short_url': newUrlModel.short_url,
    'original_url': clientReqUrl,
    'suffix': short_id

});

app.get('/api/shorturl/:input', (req, res) => {
  let input = req.params.input
  console.log(input, "<= this is the input")

  ShortUrl.findOne({ suffix: input }, (error, result) => {
    if (!error && result != undefined){
      res.redirect(result.original_url)
    }
  })

})

newUrlModel.save(function (err, doc){
  if (err) return console.error(err);
  console.log('Doc saved')
  
});



app.post('/api/shorturl', function (req, res) {

  });

});

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

Your project link(s)

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: URL Shortener Microservice

Link to the challenge:

short_url property, returned in the json is supposed to be only the shortened part, not the full url, including project address.

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