From c0ef2caec708d019ab8b5d488aee4479c6c4a7ba Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Mar 2025 09:44:07 +0000 Subject: [PATCH] =?UTF-8?q?Do=20task=20=E2=80=9CChain=20Middleware=20to=20?= =?UTF-8?q?Create=20a=20Time=20Server=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myApp.js | 25 ------------------------- scripts/time_server.js | 31 +++++++++++++++++++++++++++++++ server.js | 2 +- 3 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 scripts/time_server.js diff --git a/myApp.js b/myApp.js index cad7930..c867152 100644 --- a/myApp.js +++ b/myApp.js @@ -79,31 +79,6 @@ TestApps[3] = class TestApp3 { } } -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() { diff --git a/scripts/time_server.js b/scripts/time_server.js new file mode 100644 index 0000000..f932339 --- /dev/null +++ b/scripts/time_server.js @@ -0,0 +1,31 @@ +let express = require('express'); +require('dotenv').config(); +let app = express(); + +class TimeServer { + #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.json(this.response(REQUEST, RESPONSE));}); + } + + /* Create the default response. */ + response(REQUEST, RESPONSE) { + let DEFAULT = {'time': REQUEST.time.toString()}; + return DEFAULT; + } + + constructor() { + this.handle(); + } +} + +new TimeServer(); + +module.exports = app; \ No newline at end of file diff --git a/server.js b/server.js index f0f55fe..33c1f72 100644 --- a/server.js +++ b/server.js @@ -4,7 +4,7 @@ * ***************************************************/ const bGround = require('fcc-express-bground'); -const myApp = require('./scripts/echo'); +const myApp = require('./scripts/time_server'); const express = require('express'); const app = express();