From 85f217f63e248ae0df44368a6a0d03620955f170 Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Wed, 18 Oct 2023 03:48:52 -0400 Subject: Created initial backend architecture and redirection routes --- utils.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 utils.js (limited to 'utils.js') 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; -- cgit v1.2.3