From 5a1be2a97225f82c8c19b6634cee2aec6ee15238 Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Mar 2025 09:54:29 +0000 Subject: [PATCH] =?UTF-8?q?Complete=20task=20=E2=80=9CGet=20Query=20Parame?= =?UTF-8?q?ter=20Input=20from=20the=20Client=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/names.js | 41 +++++++++++++++++++++++++++++++++++++++++ server.js | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 scripts/names.js diff --git a/scripts/names.js b/scripts/names.js new file mode 100644 index 0000000..d2ef25a --- /dev/null +++ b/scripts/names.js @@ -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; diff --git a/server.js b/server.js index 33c1f72..ff51ffb 100644 --- a/server.js +++ b/server.js @@ -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();