URL Shortener



Redirecting...

Please wait...

Redirecting in 5 seconds
const express = require("express"); const app = express(); const PORT = 3000; app.use(express.json()); app.use(express.static("public")); app.set("view engine", "ejs"); app.set("views", __dirname + "/views"); // In-memory storage const urls = {}; app.post("/shorten", (req, res) => { const { longUrl, customAlias } = req.body; let shortCode = customAlias || Math.random().toString(36).substring(2, 8); urls[shortCode] = longUrl; res.json({ shortUrl: `http://localhost:${PORT}/${shortCode}` }); }); // Landing page with countdown app.get("/:code", (req, res) => { const code = req.params.code; const longUrl = urls[code]; if (longUrl) { res.render("landing", { longUrl }); } else { res.status(404).send("Link not found!"); } }); app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}`));