is there a way to make an url shortener or at least any examples out there in glitch
1 Like
Sorry but I don’t get it
I am starting totally from scratch
and it doesn’t make sense
Server Code
const Base = require("errors-base").default;
const urls = [];
app.get("/:id", (req, res) => {
const index = Base.decode(req.params.id);
if (!urls[id]) return res.status(404).end("404 not found");
res.redirect(urls[id]);
}):
app.put("/", (req, res) => {
const index = urls.length;
urls.push(req.body.url);
const id = Base.encode(index);
res.set("Content-Type", "application/json");
res.end(JSON.stringify(id));
});
Client Code:
Add URL:
const addURL = url => new Promise(async (resolve, reject) => fetch("/", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ url })
}).then(response => response.json()).then(id => resolve(id)).catch(reject));
Example:
const id = await addURL("https://google.com");
console.log(id);
1 Like
So that’s all I do …
That should be a working URL-shortener, but it only saves in memory. You’d have to work on this to get it working properly.
1 Like