Do task “Chain Middleware to Create a Time Server”

This commit is contained in:
buzzcode2007 2025-03-08 09:44:07 +00:00
parent c841311ad7
commit c0ef2caec7
3 changed files with 32 additions and 26 deletions

View file

@ -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() {

31
scripts/time_server.js Normal file
View file

@ -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;

View file

@ -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();