2019-11-23 19:06:31 +00:00
|
|
|
const fs = require("fs"),
|
|
|
|
emoji = require("../misc/emoji_list.json");
|
|
|
|
|
|
|
|
// Defines the function of this module
|
|
|
|
const MODULE_FUNCTION = "handle_requests",
|
|
|
|
|
|
|
|
// Base path for module folder creation and navigation
|
|
|
|
BASE_PATH = "/example_request_handler";
|
|
|
|
|
|
|
|
// Only ran on startup so using sync functions is fine
|
|
|
|
// Makes the folders for files of the module
|
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH)) {
|
|
|
|
fs.mkdirSync(__dirname + BASE_PATH);
|
2020-01-02 23:15:00 +00:00
|
|
|
console.log(`[example_request_handler] Made example_request_handler module folder`);
|
2019-11-23 19:06:31 +00:00
|
|
|
}
|
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH + "/files")) {
|
|
|
|
fs.mkdirSync(__dirname + BASE_PATH + "/files");
|
2020-01-02 23:15:00 +00:00
|
|
|
console.log(`[example_request_handler] Made example_request_handler module files folder`);
|
2019-11-23 19:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-01-03 00:31:03 +00:00
|
|
|
extras:async function() {
|
2020-01-04 12:30:27 +00:00
|
|
|
/*
|
|
|
|
Anything else that needs to be loaded into the framework
|
|
|
|
can be done here, this is used for things like busboy that
|
|
|
|
need to be put to the express server to work
|
|
|
|
The express server is accessable from global.app
|
|
|
|
*/
|
2019-11-23 20:33:53 +00:00
|
|
|
},
|
2020-01-03 00:31:03 +00:00
|
|
|
get:async function(req, res) {
|
2019-11-23 19:06:31 +00:00
|
|
|
/*
|
|
|
|
req - Request from client
|
|
|
|
res - Response from server
|
|
|
|
*/
|
2020-01-04 12:30:27 +00:00
|
|
|
|
|
|
|
// Anything that needs to be done in a GET request can be done here
|
2019-11-23 19:06:31 +00:00
|
|
|
|
2020-01-04 12:16:38 +00:00
|
|
|
// Make sure the file exists
|
2019-11-23 19:06:31 +00:00
|
|
|
fs.access(__dirname + BASE_PATH + "/files" + req.url, fs.F_OK, error => {
|
|
|
|
if (error) {
|
2020-01-04 12:16:38 +00:00
|
|
|
// File doesn't exist, return a 404 to the client.
|
2019-11-23 19:06:31 +00:00
|
|
|
global.modules.consoleHelper.printWarn(emoji.page, `${req.method}: ${req.url} was requested - Returned 404`);
|
|
|
|
res.status(404).send("404!<hr>Revolution");
|
|
|
|
} else {
|
2020-01-04 12:16:38 +00:00
|
|
|
// File does exist, send the file back to the client.
|
2019-11-23 19:06:31 +00:00
|
|
|
global.modules.consoleHelper.printInfo(emoji.page, `${req.method}: ${req.url} was requested`);
|
|
|
|
res.sendFile(__dirname + BASE_PATH + "/files" + req.url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2020-01-03 00:31:03 +00:00
|
|
|
post:async function(req, res) {
|
2019-11-23 19:06:31 +00:00
|
|
|
/*
|
|
|
|
req - Request from client
|
|
|
|
res - Response from server
|
|
|
|
*/
|
|
|
|
|
2020-01-04 12:30:27 +00:00
|
|
|
// Anything that needs to be done with a POST can be done here.
|
2019-11-23 19:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.MOD_FUNC = MODULE_FUNCTION;
|