From 82a6611297e3a5c18e767ec60d54b5a9648331cd Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Mar 2025 09:26:57 +0000 Subject: [PATCH] Create tasks 3 and 4 --- myApp.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/myApp.js b/myApp.js index 7fab971..cad7930 100644 --- a/myApp.js +++ b/myApp.js @@ -1,7 +1,10 @@ let express = require('express'); +require('dotenv').config(); let app = express(); -class TestApp1 { +const TestApps = {}; + +TestApps[1] = class TestApp { message = "Hello Express"; default_path = "/"; @@ -20,6 +23,100 @@ class TestApp1 { } } -new TestApp1(); +TestApps[2] = class TestApp { + paths = { + "assets": "/public", + "receive": "/", + "send": "/views/index.html" + }; + + /* Create a default response. */ + respond(HANDLER) { + HANDLER.sendFile(__dirname + this.paths['send']); + }; + + /* Handle requests and send default responses. */ + handle() { + app.use(this.paths['assets'], express.static(__dirname + this.paths['assets'])); + app.get(this.paths["receive"], (REQUEST, RESPONSE) => {this.respond(RESPONSE);}) + }; + + constructor() { + this.handle(); + } +} + +TestApps[3] = class TestApp3 { + paths = { + "assets": "/public", + "receive": "/", + "send": "/views/index.html", + "API": "/json" + }; + defaults = { + "response": {"message": "Hello json"} + } + + /* Create a default JSON response. */ + respond(HANDLER) { + HANDLER.json(this.defaults[`response`]); + }; + + /* Create a default HTML response. */ + create(HANDLER) { + HANDLER.json(this.defaults[`response`]); + }; + + /* Handle requests and send default responses. */ + handle() { + app.use(this.paths['assets'], express.static(__dirname + this.paths['assets'])); + app.get(this.paths["receive"], (REQUEST, RESPONSE) => {this.create(RESPONSE);}) + app.get(this.paths["API"], (REQUEST, RESPONSE) => {this.respond(RESPONSE);}) + }; + + constructor() { + this.handle(); + } +} + +TestApps[4] = class TestApp { + #paths = { + "API": { + "now": "/now" + } + } + + /* Handle API responses. + */ + handle() { + app.get(this.#paths[`API`][`now`], (REQUEST, RESPONSE, next) => {REQUEST.time = new Date(); next()}, (REQUEST, RESPONSE) => {RESPONSE.send(this.response(REQUEST, RESPONSE));}); + app.post(this.#paths[`API`][`now`], (REQUEST, RESPONSE, next) => {REQUEST.time = new Date(); next()}, (REQUEST, RESPONSE) => {RESPONSE.send(this.response(REQUEST, RESPONSE));}); + } + + /* Create the default response. */ + response(REQUEST, RESPONSE) { + let DEFAULT = {'time': REQUEST.time.toString()}; + return DEFAULT; + } + + constructor() { + this.handle(); + } +} + + +class TestApp { + test() { + new TestApps[4](); + } + + constructor() { + console.log(process.env); + this.test(); + } + +} + +new TestApp(); module.exports = app;