Create tasks 3 and 4
This commit is contained in:
parent
e75a788578
commit
82a6611297
1 changed files with 99 additions and 2 deletions
101
myApp.js
101
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue