Back End Development and APIs Back End Development and APIs Projects-URL Shortener Microservice

What could be the problem here, the app is connected successfully with the database and documents created:

require(‘dotenv’).config();
const bodyParser = require(‘body-parser’);
var validUrl = require(‘valid-url’);
const express = require(‘express’);
const cors = require(‘cors’);
const app = express();
const mongoose = require(‘mongoose’);
mongoose.connect(process.env[‘MONGO_URL’], {useNewUrlParser: true, useUnifiedTopology: true});

mongoose.connection.on(‘error’, () => {
console.log(“No connection:”);
});
mongoose.connection.once(‘open’, () =>{
console.log(“MongoDB database connection established successfully:”)
})
const urlSchema = new mongoose.Schema({
original_url: String,
short_url: String
})
const URL = mongoose.model(“URL”, urlSchema);
// Basic Configuration
const port = process.env.PORT || 3000;

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

app.get(‘/’, function(req, res) {
res.sendFile(process.cwd() + ‘/views/index.html’);
});
app.post(“/api/shorturl”, async function (req, res) {
const url = req.body.url_input;
const urlCode = Math.floor(Math.random() * 10);
if(!validUrl.isWebUri(url)){
res.json({error: “invalid url”})
}else{
try {
let findOne = await URL.findOne({
original_url: url
})
if(findOne){
res.json({
original_url: findOne.original_url,
short_url: findOne.short_url
})
}else{
findOne = new URL({
original_url: url,
short_url: urlCode
})
await findOne.save();
console.log(“doc created…”)
res.json({
original_url: findOne.original_url,
short_url: findOne.short_url
})
}
} catch (error) {
console.error(error);
res.json(‘Server error…’)
}
}
})
// Your first API endpoint
app.get(‘/api/shorturl/:short_url?’, async function(req, res) {
try {
const urlParams = await URL.findOne({
short_url: req.params.short_url
})
if (urlParams) {
return res.redirect(urlParams.original_url);
console.log(“Sending to the original link”)
}else{
return res.json(“No URL Found”);
}
} catch (error) {
console.error(error);
res.json(“Server error…”);
}
});

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

this two tests are not passing, any help is appreciated.

// running tests You can POST a URL to

/api/shorturl

and get a JSON response with

original_url

and

short_url

properties. Here’s an example:

{ original_url : 'https://freeCodeCamp.org', short_url : 1}

When you visit

/api/shorturl/<short_url>

, you will be redirected to the original URL. // tests completed

You can POST a URL to

/api/shorturl

and get a JSON response with

original_url

and

short_url

properties. Here’s an example:

{ original_url : 'https://freeCodeCamp.org', short_url : 1}

When you visit

/api/shorturl/<short_url>

, you will be redirected to the original URL.

Replit link

Log your POST url route inputs and all possible responses (you’re only logging part of them) and you will see output like:

req.body: {"url":"https://boilerplate-project-urlshortener.jeremyagray.repl.co/?v=1673612640168"}
req.params: {}
req.query: {}
invalid URL
req.body: {"url":"https://boilerplate-project-urlshortener.jeremyagray.repl.co/?v=1673612640486"}
req.params: {}
req.query: {}
invalid URL
req.body: {"url":"ftp:/john-doe.invalidTLD"}
req.params: {}
req.query: {}
invalid URL

So your code thinks all the URLs used for testing are invalid.

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