-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
195 lines (180 loc) · 5.72 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// server.js
if (process.env.NODE_ENV != "production") {
require("dotenv").config();
}
// Main dependencies
const express = require("express");
const app = express();
const server = require("http").Server(app);
const convert = require("xml-js");
const colourConvert = require("color-convert");
// Handle 'sitemap.xml' and 'robots.txt' functionality so crawl bots can access them.
app.get("/robots.txt", function (req, res) {
res.sendFile(__dirname + "/robots.txt");
});
app.get("/sitemap.xml", function (req, res) {
res.sendFile(__dirname + "/sitemap.xml");
});
// If the user has a trailing '/' at the end of a URL, remove it and refresh.
app.use((req, res, next) => {
if (req.path.charAt(req.path.length - 1) === "/" && req.path.length > 1) {
const query = req.url.slice(req.path.length);
const safepath = req.path.slice(0, -1).replace(/\/+/g, "/");
res.redirect(301, safepath + query);
} else {
next();
}
});
// Declare ejs, JSON formatting and set static files folder.
app.set("view engine", "ejs");
app.set("json spaces", 2);
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Home
app.get("/", (req, res) => {
// Render the page with given paramaters.
res.render("index", {
title: "Home",
});
});
app.get("/code-formatter", (req, res) => {
// Render the page with given paramaters.
res.render("formatters/code-formatter", {
title: "Code Formatter",
});
});
app.get("/hex-to-filter", (req, res) => {
// Render the page with given paramaters.
res.render("converters/hex-to-filter", {
title: "Hex to Filter",
});
});
app.get("/image-converter", (req, res) => {
// Render the page with given paramaters.
res.render("converters/image-converter", {
title: "Image Converter",
});
});
app.get("/lorem-ipsum-generator", (req, res) => {
// Render the page with given paramaters.
res.render("text/lorem-ipsum-generator", {
title: "Lorem Ipsum Generator",
});
});
app.get("/word-counter", (req, res) => {
// Render the page with given paramaters.
res.render("text/word-counter", {
title: "Word Counter",
});
});
app.get("/colour-converter", (req, res) => {
// Render the page with given paramaters.
res.render("converters/colour-converter", {
title: "Colour Converter",
});
});
app.get("/color-converter", (req, res) => {
// Render the page with given paramaters.
res.redirect("/colour-converter");
});
app.get("/json-xml-converter", (req, res) => {
// Render the page with given paramaters.
res.render("converters/json-xml-converter", {
title: "Json to XML Converter",
});
});
app.get("/epoch-time-converter", (req, res) => {
// Render the page with given paramaters.
res.render("converters/epoch-time-converter", {
title: "Epoch to Standard Date Time Converter",
});
});
/* POST Requests */
app.post("/convert-colour", (req, res) => {
// Get the colour and the input value, minus any non numbers.
var colour = "";
const inputValue = req.body.focusedInputValue.replace(/[^\d,-]/g, "");
// Depending on the colour type, convert it to hex to use later.
if (req.body.focusedInputType == "hex") {
colour = req.body.focusedInputValue.substring(1);
} else if (req.body.focusedInputType == "rgb") {
const splitArray = inputValue.split(",");
console.log(splitArray);
colour = colourConvert.rgb.hex(
parseInt(splitArray[0]),
parseInt(splitArray[1]),
parseInt(splitArray[2])
);
} else if (req.body.focusedInputType == "hsl") {
const splitArray = inputValue.split(",");
colour = colourConvert.hsl.hex(
parseInt(splitArray[0]),
parseInt(splitArray[1]),
parseInt(splitArray[2])
);
} else if (req.body.focusedInputType == "hwb") {
const splitArray = inputValue.split(",");
colour = colourConvert.hwb.hex(
parseInt(splitArray[0]),
parseInt(splitArray[1]),
parseInt(splitArray[2])
);
}
// Put all of the converted colours in an object.
const colourObj = {};
colourObj.hex = colour;
colourObj.rgb = colourConvert.hex.rgb(colour);
colourObj.hsl = colourConvert.hex.hsl(colour);
colourObj.hwb = colourConvert.hex.hwb(colour);
// Send the converted colours back to the client.
res.status(200).json({ colourObj: colourObj });
});
app.post("/convert-json-data", (req, res) => {
// Get the conversion type and code, while initialisng the result
const conversionType = req.body.conversionType;
const toConvertCode = req.body.toConvertCode;
var result = "";
// Remove the "_text" key that the 'xml2json' function creates
var removeJsonTextAttribute = function (value, parentElement) {
try {
var keyNo = Object.keys(parentElement._parent).length;
var keyName = Object.keys(parentElement._parent)[keyNo - 1];
parentElement._parent[keyName] = nativeType(value);
} catch (e) {}
};
// Check for native type.
function nativeType(value) {
var nValue = Number(value);
if (!isNaN(nValue)) {
return nValue;
}
var bValue = value.toLowerCase();
if (bValue === "true") {
return true;
} else if (bValue === "false") {
return false;
}
return value;
}
// Check for the conversion type and convert the correct code.
if (conversionType == "javascript") {
result = convert.xml2json(toConvertCode, {
compact: true,
ignoreComment: true,
textFn: removeJsonTextAttribute,
spaces: 4,
});
} else if (conversionType == "xml") {
result = convert.json2xml(toConvertCode, {
compact: true,
ignoreComment: true,
spaces: 4,
});
}
// Send the converted code back to the client.
res.status(200).json({ result: result });
});
// Initialise the server on port 3000.
const PORT = process.env.PORT || 3000;
server.listen(PORT);