Unable to pass APIs and Microservices Projects - URL Shortener Microservice

Tell us what’s happening:

I tested the function of my code and it satisfies the requirement but it does not show that I complete the test after I put it in freecodecamp website. Can someone please help me check it? Thank you.

Your code so far
in server.js

‘use strict’;

var express = require(‘express’);
var mongo = require(‘mongodb’);
var mongoose = require(‘mongoose’);
const dns = require(‘dns’)

var cors = require(‘cors’);
require(“dotenv”).config();

var app = express();

var 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’);
});

app.get("/api/hello", function (req, res) {
res.json({greeting: ‘hello API’});
});

app.listen(port, function () {
console.log(‘Node.js listening …’);
});

const uri = process.env.ATLAS_URI
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
const connection = mongoose.connection;
connection.once(“open”, () => {
console.log(“MongoDB database connection established successfully”);
});

let urlSchema = new mongoose.Schema({
original : {type: String, required: true},
short: Number
})

let Url = mongoose.model(‘Url’, urlSchema)

let bodyParser = require(‘body-parser’)
let responseObject = {}
app.post(’/api/shorturl/new’, bodyParser.urlencoded({ extended: false }) , (request, response) => {
let inputUrl = request.body[‘url’]

dns.lookup(inputUrl, (err, addresses, family)=>{

if(err){
  return response.json({error: "invalid url"})
} else {
  responseObject['original_url'] = inputUrl
  let inputShort = 1
  
  Url.findOne({})
    .sort({short: 'desc'})
    .exec((error, result) => {
      if(!error && result != undefined){
        inputShort = result.short + 1
      }
      if(!error){
        Url.findOneAndUpdate(
          {original: inputUrl},
          {original: inputUrl, short: inputShort},
          {new: true, upsert: true },
          (error, savedUrl)=> {
            if(!error){
              responseObject['short_url'] = savedUrl.short
              response.json(responseObject)
            }
          }
        )
      }

})

}

})

})

app.get(’/api/shorturl/:input’, (request, response) => {
var urlInput = request.params.input;
console.log(urlInput);

Url.findOne({‘short’: urlInput}, (error, result) => {
if(error) return response.send(‘Error reading database’);

  var re = new RegExp("^(http|https)://", "i");
  var strToCheck = result.original
if(re.test(strToCheck)){
    response.redirect(301, result.original);
}else{
    response.redirect(301, 'http://' + result.original);
}

})
})

Your browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

Are you using repl.it? If so, can you share the link to the project?

1 Like

I have exactly the same problem, I want to use DNS library cause other options cause a bug, for example, if I enter the https://www.askljdalksjd.com it saved the database and no response like {error: invalid url}. If I use dns.lookup I don’t face any problem like that

[https://repl.it/@mihalidis/FCC-Url-Shortener](Here is my project)

1 Like

Continuing the discussion from Unable to pass APIs and Microservices Projects - URL Shortener Microservice:

Thank you for reaching out. Here is my repl.it link: https://repl.it/@chloechloew/boilerplate-project-urlshortener

1 Like

Thank you for reaching out. Here is my repl.it link: https://repl.it/@chloechloew/boilerplate-project-urlshortener

1 Like