This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.

38 lines
1.4 KiB
JavaScript

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const database = require("./config/database");
const ShortenedLink = require("./models/ShortenedLink");
const app = express();
database.authenticate()
.then(() => console.log("Connected to " + process.env.MYSQL_HOSTNAME + " with user " + process.env.MYSQL_USERNAME))
.catch(err => console.log("An error occurred while connecting to database: " + err));
app.use(cors());
app.get("/:shortedID", async (req, res) => {
const link = await ShortenedLink.findOne({where: {shorten_url: req.params.shortedID}});
if (link) {
if (link.show_meta_data) {
let html = "<html><head>";
html += getMeta("og:title", link.meta_title);
html += getMeta("og:description", link.meta_description);
html += getMeta("og:image", link.meta_image);
html += getMeta("theme-color", link.meta_color);
html += '<meta http-equiv="refresh" content="0; url=' + link.original_url + '" />';
html += "</head></html>";
res.status(200).send(html);
} else res.redirect(link.original_url);
} else res.status(404).json({message: "The specified link wasn't found"});
});
function getMeta(name, content) {
return '<meta name="' + name + '" content="'+content+'" />';
}
app.get('*', (req, res) => {
res.redirect("https://sheepstar.xyz/");
});
app.listen(process.env.SERVER_PORT || 3000);