Make handling the api more sensible

This commit is contained in:
tgpethan 2020-05-21 13:15:39 +01:00
parent 8b063ea75f
commit 690fdad2fc

72
EUS.js
View file

@ -86,43 +86,41 @@ module.exports = {
res.set("X-Content-Type-Options", "nosniff"); res.set("X-Content-Type-Options", "nosniff");
// Check if returned value is true. // Check if returned value is true.
if (!req.url.includes("/api/")) { if (req.url.includes("/api/")) return handleAPI(req, res);
// Register the time at the start of the request
d = new Date(); // Register the time at the start of the request
startTime = d.getTime(); d = new Date();
// Get the requested image startTime = d.getTime();
let urs = ""+req.url; urs = urs.split("/")[1]; // Get the requested image
// Get the file type of the image from image_json and make sure it exists let urs = ""+req.url; urs = urs.split("/")[1];
fs.access(__dirname + BASE_PATH + "/i/"+urs+image_json[urs], error => { // Get the file type of the image from image_json and make sure it exists
if (error) { fs.access(__dirname + BASE_PATH + "/i/"+urs+image_json[urs], error => {
// Doesn't exist, handle request normaly if (error) {
if (req.url === "/") { urs = "/index.html" } else { urs = req.url }; // Doesn't exist, handle request normaly
fs.access(__dirname + BASE_PATH + "/files"+urs, error => { if (req.url === "/") { urs = "/index.html" } else { urs = req.url };
if (error) { fs.access(__dirname + BASE_PATH + "/files"+urs, error => {
// Doesn't exist, send a 404 to the client. if (error) {
res.status(404).end("404!"); // Doesn't exist, send a 404 to the client.
d = new Date(); res.status(404).end("404!");
endTime = d.getTime(); d = new Date();
global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`); endTime = d.getTime();
} else { global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`);
// File does exist, send it back to the client. } else {
res.sendFile(__dirname + BASE_PATH + "/files"+req.url); // File does exist, send it back to the client.
d = new Date(); res.sendFile(__dirname + BASE_PATH + "/files"+req.url);
endTime = d.getTime(); d = new Date();
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); endTime = d.getTime();
} global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`);
}); }
} else { });
// Image does exist, send it back. } else {
res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]); // Image does exist, send it back.
d = new Date(); res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]);
endTime = d.getTime(); d = new Date();
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`); endTime = d.getTime();
} global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`);
}); }
} else { });
return handleAPI(req, res);
}
}, },
post:function(req, res) { post:function(req, res) {
/* /*