summaryrefslogtreecommitdiff
path: root/utils.js
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2023-10-18 03:48:52 -0400
committerCarson Fleming <cflems@cflems.net>2023-10-18 03:48:52 -0400
commit85f217f63e248ae0df44368a6a0d03620955f170 (patch)
treed6d84084f8f2ad5a3fa0804ee576b492d9e33e7e /utils.js
parentadaf5b0fde4431eb186075e7921e2f0999e68dd9 (diff)
downloadfle.ms-85f217f63e248ae0df44368a6a0d03620955f170.tar.gz
Created initial backend architecture and redirection routes
Diffstat (limited to 'utils.js')
-rw-r--r--utils.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/utils.js b/utils.js
new file mode 100644
index 0000000..7ea6b42
--- /dev/null
+++ b/utils.js
@@ -0,0 +1,33 @@
+const path = require('path');
+const config = require('./config.js');
+
+const helperFunctions = {
+ sendNotFoundError : function (res) {
+ res.status(404).sendFile('static/notfound.html', {root: path.join(__dirname)},
+ err => res.json(
+ {status: "ERROR", statusCode: 404, errorType: "NOT FOUND", errorMessage: "Not Found"}
+ ));
+ },
+ sendBadRequestError : function (res, errorMessage) {
+ res.status(400).json({status: "ERROR", statusCode: 400, errorType: "BAD REQUEST", errorMessage});
+ },
+ sendInternalServerError : function (res) {
+ res.status(404).json({status: "ERROR", statusCode: 500, errorType: "INTERNAL", errorMessage: "Internal Server Error"});
+ },
+ sendSuccessfulSlugCreation : function (res, slug) {
+ res.status(201).json({status: "SUCCESS", statusCode: 201, data: {shortUrl: config.BASE_URI+slug}});
+ },
+ isAlphaNumeric : function (str) {
+ return str.match(/^[a-zA-Z0-9]+$/) !== null;
+ },
+ isUrlFormatted : function (str) {
+ try {
+ new URL(str);
+ return true;
+ } catch (err) {
+ return false;
+ }
+ }
+};
+
+module.exports = helperFunctions;