Url Project not getting passed although the results match


this is my url where the code is written
here is the code :require(‘dotenv’).config();
const express = require(‘express’);
const cors = require(‘cors’);
const app = express();
var bodyParser=require(‘body-parser’);
const mongoose = require(‘mongoose’);
var dns = require(‘dns’);
var btoa = require(‘btoa’);
var atob = require(‘atob’);
app.use(bodyParser.urlencoded({ extended: false }));

app.use(bodyParser.json());
try{
var promise=mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
console.log(“connected”);
console.log(mongoose.connection.readyState);
const options = {
all:true,
};
if(mongoose.connection.readyState)
{
const Schema = mongoose.Schema;

var countersSchema = new mongoose.Schema({
_id: { type: String, required: true },
count: { type: Number, default: 0 }
});
var urlSchema = new Schema({
_id: {type: Number},
url: ‘’,
created_at: ‘’
});

var Counter = mongoose.model(‘Counter’, countersSchema);
urlSchema.pre(‘save’, function(next) {
console.log(‘running pre-save’);
var doc = this;
Counter.findByIdAndUpdate({ _id: ‘url_count’ }, { $inc: { count: 1 } }, function(err, counter) {
if(err) return next(err);
console.log(counter);
console.log(counter.count);
doc._id = counter.count;
doc.created_at = new Date();
console.log(doc);
next();
});
});
var URL = mongoose.model(‘URL’,urlSchema);

promise.then(function(db) {
console.log(‘connected!’);
URL.remove({}, function() {
console.log(‘URL collection removed’);
})
Counter.remove({}, function() {
console.log(‘Counter collection removed’);
var counter = new Counter({_id: ‘url_count’, count: 1});
counter.save(function(err) {
if(err) return console.error(err);
console.log(‘counter inserted’);
});
});
})
app.get(’/api/shorturl/:new’, function(req, res) {
console.log(“inside get”);
var baseid=req.params.new;
var id = baseid;
URL.findOne({_id: id}, function(err, doc) {
if(err){
res.json ( {“error”:“No short URL found for the given input”});
}

  if(doc){
    
  res.redirect(doc.url) ;

  }else{
    res.redirect('/');
  }
  

});

//res.json({ greeting: ‘hello API’ });
});

app.post("/api/shorturl/new", function(req, res) {
console.log(“inside Post”);
console.log(“url “+req.body.url);
var url=req.body.url;
var actualurl = url.replace(“https://”, “”);
actualurl=actualurl.replace(“http://”,””);
var slashIndex = actualurl.indexOf("/");
var dnsUrl = slashIndex < 0 ? actualurl : actualurl.slice(0, slashIndex);
const options = {

// Setting family as 6 i.e. IPv6 
family: 0, 
hints: dns.ADDRCONFIG | dns.V4MAPPED, 

};
console.log(“act utl”+actualurl);
var w3 = dns.lookup(dnsUrl,function (err, addresses, family) {
if(err){
console.log(“error”+err);
return res.json({error:“invalid URL”});

}

console.log(“save 1”);

URL.findOne({url: url}, function(err, doc) {
var orginalUrl = new URL({url:url });
if(doc) {
console.log(‘entry found in db’);

        res.json({
            original_url: req.body.url,
            short_url: doc._id,
           
        });
    }
    else{

console.log(“new Entry”);
orginalUrl.save(function(err, data) {
console.log(“save 2”);
console.log(url);
console.log( req.body.url);
if (err) return console.log(err);
res.json({
original_url: req.body.url,
short_url: orginalUrl._id

            });
//done(null, data)

});

    }

});

});

});
}
}catch(e){

console.log(“conection failed”);
}

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

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

I am able to get the same output as mentioned in the project . but the application fails to pass through

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