Hi everyone, this is my firs topic on FCC, until now i’ve always found help from your posts.
In any case, i have a problem with the third test of this challenge:
If you pass an invalid URL that doesn’t follow the valid
http://www.example.com
format, the JSON response will contain{ error: 'invalid url' }
Even if i use dns.lookup or valid-url i not pass this test, and when i insert a wrong link to the url shortener site, i got an error message like this:
TypeError [ERR_INVALID_URL]: Invalid URL: fersa
at onParseError (internal/url.js:258:9)
at new URL (internal/url.js:334:5)
at /home/runner/Barabba-URL-Shortener-FCC/server.js:45:16
at Layer.handle [as handle_request] (/home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/layer.js:95:5)
at /home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/index.js:335:12)
at next (/home/runner/Barabba-URL-Shortener-FCC/node_modules/express/lib/router/index.js:275:10)
but not a json response…
Here the code:
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const dns = require('dns');
const URL = require('url').URL;
const { nanoid } = require('nanoid');
const MONGO_URI="mongodb+srv://BarabbaFDM:" + process.env['PW'] + "@cluster0.43tgt.mongodb.net/db1?retryWrites=true&w=majority";
mongoose.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
// 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');
});
//Schema and model to store saved URLS
var ShortURL = mongoose.model('ShortURL', new mongoose.Schema({
original_url: String,
short_url: String,
}));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
//Post data to the server, request URL, check if it is valid,
//send the shortened url to DB
app.post("/api/shorturl/", (req, res, next) => {
let original_url = req.body.url;
//Variable for dns.lookup
let newURL = new URL(original_url);
//Validate URL
dns.lookup(newURL.hostname, (err, address, family) => {
if (err) {
res.json({
error: 'invalid url'
});
} else {
let short_url = nanoid()
//Create an object and save it to the DB
let newURL = new ShortURL({
original_url: original_url,
short_url: short_url
});
newURL.save((err, data) => {
if (err) return console.error(err);
});
res.json({
original_url: original_url,
short_url: short_url
});
};
});
});
app.get("/api/shorturl/:short_url", (req, res) => {
let short_url = req.params.short_url;
ShortURL.findOne({short_url: short_url}, (err, data) => {
if (err) return console.log(err);
res.redirect(301, data.original_url);
});
});
// 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}`);
});
Thank you everyone for your help!
Your project link(s)
code: Barabba - URL Shortener - FCC - Replit
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0
Challenge: URL Shortener Microservice
Link to the challenge: