Added the shortener
This commit is contained in:
38
ShortenerV1/app.js
Normal file
38
ShortenerV1/app.js
Normal file
@ -0,0 +1,38 @@
|
||||
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);
|
Reference in New Issue
Block a user