-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
101 lines (84 loc) · 3.29 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require("express");
const logger = require("morgan");
const chalk = require("chalk");
const path = require("path");
const fs = require('fs');
const bodyParser = require("body-parser");
const ob = require('./HTML.json');
var obj = require('./cssNames.json');
const removeEmptyLines = require("remove-blank-lines");
const thiccify = require('./thiccify.js');
var app = express();
// clear console for clean output
console.clear();
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
var port = process.env.PORT || 4000;
app.set("views", "./views");
app.use(logger("dev"));
app.use(express.static(path.join(__dirname, "./public")));
app.get("/", function (req, res) {
return res.sendFile(path.join(__dirname, './views/index.html'));
});
app.post("/", function (req, res, next) {
var code = req.body.code;
// console.log(code);
var results = {};
var run;
if (req.body.codeType === 'css') {
try {
//Does all the css crap that nobody should every have to read.
let data = removeEmptyLines(code);
data = data.replace(new RegExp('{', 'g'), '{\n');
data = data.replace(new RegExp('}', 'g'), '\n}\n');
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
var comment = "\n\t/*" + obj[property] + "*/\n\t" + property + ":";
data = data.replace(new RegExp(" " + property + ":", 'g'), comment);
}
}
results.code = data;
results.msg = "Thiccified: " + (((results.code.length / code.length) * 10000) | 0) / 100 + "% of original size";
return res.status(200).json(results);
} catch (error) {
return res.status(500).json(error)
}
} else if (req.body.codeType === 'html') {
try {
let data = code;
//let results = data;
data = data.replace(/<!--[\S ]*-->/g, '');
for (var property in ob) {
if (ob.hasOwnProperty(property)) {
// console.log(prop);
var prop = property.slice(0, -1);
var comment = "\n<!-- " + ob[property] + "-->\n" + prop;
//data = data.replace(new RegExp(prop+'[> ]', 'g'), comment);
data = data.replace(new RegExp(prop + '>', 'g'), comment + '>');
data = data.replace(new RegExp(prop + ' ', 'g'), comment + ' ');
}
}
results.code = data;
results.msg = "Thiccified: " + (((results.code.length / code.length) * 10000) | 0) / 100 + "% of original size";
return res.status(200).json(results);
} catch (error) {
return res.status(500).json(error)
}
} else {
try {
run = thiccify.run(code);
// console.log(run);
results = thiccify.unRoll(run.code);
results.msg = "Thiccified: " + (((results.code.length / code.length) * 10000) | 0) / 100 + "% of original size";
return res.status(200).json(results);
} catch (error) {
return res.status(500).json(error)
}
}
});
app.listen(port);
console.log(chalk.yellow("Server started! At http://localhost:" + (port)));