Revolution/modules/example_request_handler.js

55 lines
2.0 KiB
JavaScript
Raw Normal View History

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);
console.log(`[example_request_handler] Made example_request_handler module folder`);
}
if (!fs.existsSync(__dirname + BASE_PATH + "/files")) {
fs.mkdirSync(__dirname + BASE_PATH + "/files");
console.log(`[example_request_handler] Made example_request_handler module files folder`);
}
module.exports = {
2020-01-03 00:31:03 +00:00
extras:async function() {
2019-11-23 20:33:53 +00:00
// Anything else that is needed like busboy
// Put them to global.app (the express app)
},
2020-01-03 00:31:03 +00:00
get:async function(req, res) {
/*
req - Request from client
res - Response from server
*/
// Make sure the file exists
fs.access(__dirname + BASE_PATH + "/files" + req.url, fs.F_OK, error => {
if (error) {
// File doesn't exist, return a 404 to the client.
global.modules.consoleHelper.printWarn(emoji.page, `${req.method}: ${req.url} was requested - Returned 404`);
res.status(404).send("404!<hr>Revolution");
} else {
// File does exist, send the file back to the client.
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) {
/*
req - Request from client
res - Response from server
*/
// Anything that needs to be done with a post can be done here.
}
}
module.exports.MOD_FUNC = MODULE_FUNCTION;