Implemented the code for the shortener

This commit is contained in:
Mathias Wagner 2022-09-09 01:09:11 +02:00
parent cb17c65dcc
commit adee79675c
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,7 +1,26 @@
import express, {Express} from 'express';
import express, {Express, Request, Response} from 'express';
import {ShortenedLink} from "../models/ShortenedLink";
const app: Express = express();
app.get("*", async (req: Request, res: Response) => {
const link = req.originalUrl.substring(1) || "home";
let redirect_url = "https://sheepstar.xyz";
// Search provided link
const found = await ShortenedLink.findOne({shortenedId: link});
if (found != null) {
// Update redirect url
redirect_url = found.originalUrl;
// Increment clicks
await found.updateOne({$inc: {clicks: 1}}).exec();
}
// Redirect to url
res.status(301).header("Location", redirect_url).end();
});
/** Logs something with a shortener prefix */
const log = (msg: string) => console.log(`[Shortener] ${msg}`);