import { Route, Server } from '../../../class'; import { RedirectRaw } from '../../../models'; export default class Root extends Route { constructor(server: Server) { super(server); this.conf = { path: '/', }; } public bind() { this.router.get('/', (_req, res) => res.redirect('https://www.libraryofcode.org/')); this.router.get('/:key', async (req, res) => { try { const link: RedirectRaw = await this.server.client.db.Redirect.findOne({ key: req.params.key }).lean().exec(); if (!link) return res.status(404).json({ code: this.constants.codes.NOT_FOUND, message: this.constants.messages.NOT_FOUND }); res.redirect(link.to); return await this.server.client.db.Redirect.updateOne({ key: req.params.key }, { $inc: { visitedCount: 1 } }); } catch (err) { this.server.client.util.handleError(err); return res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR }); } }); } }