-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (57 loc) · 2.03 KB
/
server.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// const db = require("./db");
const express = require('express');
const app = express();
const path = require('path');
const validator = require('validator');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
const cookieParser = require('cookie-parser');
app.use(cookieParser());
// .env holds the connection info
require('dotenv').config();
const PORT = 3001;
app.use("/libs", express.static(path.join(__dirname, 'libs')));
app.use("/components", express.static(path.join(__dirname, "components")));
app.use("/audio", express.static(path.join(__dirname, "audio")));
app.get("/", async function(req, res) {
// var saveResult = await db.query('SELECT * FROM saves');
// var ip = getIp(req);
res.sendFile(path.join(__dirname, 'index.html'));
});
// app.post("/save", async function(req, res) {
// var ip = getIp(req);
// var jsonResult = req.body;
// // Overwrite old save
// var deleteResult = await db.query('DELETE FROM saves WHERE ip = $1', [ip]);
// var saveResult = await db.query('INSERT INTO saves (ip, blocks) VALUES ($1, $2)', [ip, jsonResult]);
// res.send("Success");
// });
// app.get("/load", async function(req, res) {
// var ip = getIp(req);
// var loadResult = await db.query('SELECT blocks FROM saves WHERE ip = $1', [ip]);
// var jsonResult = loadResult.rows[0].blocks.save;
// res.json(jsonResult);
// })
var server = app.listen(PORT, function() {
console.log("Listening on port " + server.address().port);
});
/**
* Retrieves the real IP
* @param {object} req Request object
* @returns {string} The ip address, 'error' if error
* @public
*/
function getIp(req) {
var header = req.headers['x-forwarded-for'];
if (!header) {
return "localhost";
}
else {
header = header.split(", ")[0]; // Will get first IP, even if only one in header
}
if (!validator.isIP(header)) {
console.log(`[${new Date()}] IP '` + header + "' was detected as invalid!");
return "error";
}
return header;
}