Do task “Chain Middleware to Create a Time Server”
This commit is contained in:
parent
c841311ad7
commit
c0ef2caec7
3 changed files with 32 additions and 26 deletions
25
myApp.js
25
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() {
|
||||
|
|
31
scripts/time_server.js
Normal file
31
scripts/time_server.js
Normal 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;
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue