Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:

i dont know what is the problem with my code. i can run it clearly without any problem, but they always show an error like this:

When you visit /api/shorturl/<short_url>, you will be redirected to the original URL.

##JavaScript

require('dotenv').config()
const dns = ("dns");
const express = ("express");
const bodyParser = ("body-parser");
const cors = ("cors")

const app = express();
app.use("/public", express.static(`${process.cwd()}/public`));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(cors())

const db = [];

app.get("/", (req, res) => {
	res.sendFile(process.cwd() + "/views/index.html");
});

app.get("/api/shorturl/:id", (req, res) => {
  const shorted = db.find(m => m.short_url == req.params.id);
	if (shorted) {
		return res.redirect(302, shorted.original_url);
	}
	return res.json({ error: "No short URL found for the given input" });
});


app.post("/api/shorturl", (req, res) => {
	const url = req.body.url.match(/https:\/\/(www.)?.*/g);
	if (!url) return res.json({ error: "invalid URL" });

	const validUrl = new URL(url[0]);
	let result = {
    original_url: url[0],
	};
  
	dns.lookup(validUrl.hostname, (error, address) => {
    if (error) return res.json(error);
		const shorted = db.find(m => url[0] === m.original_url);
		result.short_url =
    shorted?.short_url ||
    address.split(".").reduce((prev, current) => prev + Number(current), 0);
    
		res.json(result);
		if (!shorted) {
      db.push({ ...result, address });
		}
	});
});

app.listen(3000, () => {
	console.log("active");
});

Your browser information:

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

Challenge Information:

Back End Development and APIs Projects - URL Shortener Microservice

const dns = "dns";
const express = "express";
const bodyParser = "body-parser";
const cors = "cors";

Why are you doing this? You should import or use require.


Your short_url value is the same for both URLs posted. Log out what your reduce is returning and submit (you are summing the octets of the same IP address).

that’s right, i just forgot to use require in the example code, but i added the require in my local project. so it’s not a main problem

require('dotenv').config()
const dns = require("dns");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors")

const app = express();
app.use("/public", express.static(`${process.cwd()}/public`));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(cors())

const db = [];

app.get("/", (req, res) => {
	res.sendFile(process.cwd() + "/views/index.html");
});

app.get("/api/shorturl/:id", (req, res) => {
  const shorted = db.find(m => m.short_url == req.params.id);
	if (shorted) {
		return res.redirect(302, shorted.original_url);
	}
	return res.json({ error: "No short URL found for the given input" });
});


app.post("/api/shorturl", (req, res) => {
	const url = req.body.url.match(/https:\/\/(www.)?.*/g);
	if (!url) return res.json({ error: "invalid URL" });

	const validUrl = new URL(url[0]);
	let result = {
    original_url: url[0],
	};
  
	dns.lookup(validUrl.hostname, (error, address) => {
    if (error) return res.json(error);
		const shorted = db.find(m => url[0] === m.original_url);
		result.short_url =
    shorted?.short_url ||
    address.split(".").reduce((prev, current) => prev + Number(current), 0);
    
		res.json(result);
		if (!shorted) {
      db.push({ ...result, address });
		}
	});
});

app.listen(3000, () => {
	console.log("active");
});

It is for people trying to run your code. It is the first problem they encounter.

Anyway, I explained what the problem is with your code. Your short_url value is not unique, so you res back the first URL tested with instead of the next URL (the test POSTs twice and you res the URL from the first POST, not the second).

my bad, i`m not focus

thanks for your help, i know what`s the problem now