Build a URL shortening service with analytics, custom aliases, and expiration dates
Create a URL shortener API that converts long URLs into short, shareable links with tracking capabilities and optional custom aliases.
const shortid = require('shortid');
app.post('/shorten', async (req, res) => {
const { url, customAlias } = req.body;
const shortCode = customAlias || shortid.generate();
await db.urls.create({ shortCode, originalUrl: url, clicks: 0 });
res.json({ shortUrl: `${baseUrl}/${shortCode}` });
});
app.get('/:shortCode', async (req, res) => {
const url = await db.urls.findOne({ shortCode: req.params.shortCode });
await db.urls.update({ clicks: url.clicks + 1 });
res.redirect(url.originalUrl);
});