Implemented the code for the shortener
This commit is contained in:
parent
cb17c65dcc
commit
adee79675c
@ -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();
|
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 */
|
/** Logs something with a shortener prefix */
|
||||||
const log = (msg: string) => console.log(`[Shortener] ${msg}`);
|
const log = (msg: string) => console.log(`[Shortener] ${msg}`);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user