Complete task “Get Query Parameter Input from the Client”
This commit is contained in:
parent
c0ef2caec7
commit
5a1be2a972
2 changed files with 42 additions and 1 deletions
41
scripts/names.js
Normal file
41
scripts/names.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
let express = require('express'); require('dotenv').config(); let app = express();
|
||||
const bodyParser = require(`body-parser`);
|
||||
|
||||
class ServerForNames {
|
||||
#paths = {
|
||||
"API": {
|
||||
"name": "/name"
|
||||
}
|
||||
}
|
||||
|
||||
/* Echo.
|
||||
|
||||
PARAMETERS (object): The name parameters.
|
||||
returns: (object) the formatted repeat response
|
||||
*/
|
||||
parse(PARAMETERS) {
|
||||
let RESPONSE = {"name": [PARAMETERS["first"], PARAMETERS["last"]].join(` `)};
|
||||
return (RESPONSE);
|
||||
}
|
||||
|
||||
/* Create the response.
|
||||
|
||||
REQUEST (object): The request object.
|
||||
RESPONSE (object): The response object.
|
||||
*/
|
||||
respond(REQUEST, RESPONSE) {
|
||||
RESPONSE.json(this.parse(REQUEST.query));
|
||||
}
|
||||
|
||||
constructor() {
|
||||
app.use(bodyParser.urlencoded({extended: false}));
|
||||
|
||||
[`get`, `post`].forEach((METHOD) => {
|
||||
app.route(this.#paths[`API`][`name`])[METHOD]((REQUEST, RESPONSE, next) => {this.respond(REQUEST, RESPONSE);});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
new ServerForNames();
|
||||
|
||||
module.exports = app;
|
|
@ -4,7 +4,7 @@
|
|||
* ***************************************************/
|
||||
|
||||
const bGround = require('fcc-express-bground');
|
||||
const myApp = require('./scripts/time_server');
|
||||
const myApp = require('./scripts/names');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue