-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
51 lines (43 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require("express");
const cors = require("cors");
const svgBadge = require("./frontend/src/svgBadge");
const { port, mongourl } = require('./config.json');
// Create an express instance and setup middlewares
const app = express();
app.use(express.json());
app.use(cors());
app.use(express.static(__dirname + '/frontend/build'));
// Initilize mongoDB connection
const mongoose = require('mongoose');
const database = require('./database/mongo.js');
mongoose.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB.'))
.catch((err) => console.log('Unable to connect to MongoDB.\nError: ' + err));
// Disable caching
app.use(function (req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revaluniqueIDate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next()
});
async function processSVG(req, res) {
// Get values from query and parameter
const labelBGColor = req.query.labelBGColor || "484848";
const countBGColor = req.query.countBGColor || "1CA2F1";
const labelTextColor = req.query.labelTextColor || "FFFFFF";
const countTextColor = req.query.countTextColor || "FFFFFF";
const shadow = req.query.shadow || "1";
const shadowOpacity = req.query.shadowOpacity || "30";
const label = req.query.label || "VISITS";
const uniqueID = req.params.uniqueID;
const swap = req.query.swap || "0";
// Get the current visits count
const visits = await database.visitsBadge(uniqueID);
// Create the SVG Badge
let svg = svgBadge(label, shadow, shadowOpacity, swap, labelBGColor, countBGColor, labelTextColor, countTextColor, visits);
// Send the SVG Badge
res.setHeader("Content-Type", "image/svg+xml");
res.send(svg);
}
app.get("/:uniqueID", (req, res) => processSVG(req, res));
app.listen(port, () => console.log(`Ready on port ${port}.`));