From aa5c1dad387a92d6189f17b0f489272fa2bdd5d8 Mon Sep 17 00:00:00 2001 From: Vladi Markova Date: Thu, 25 Nov 2021 22:15:28 +0200 Subject: [PATCH 1/2] add solution to first part of task2 of 251121 exercise --- week7/exercise/task2-firstpart-251121.js | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 week7/exercise/task2-firstpart-251121.js diff --git a/week7/exercise/task2-firstpart-251121.js b/week7/exercise/task2-firstpart-251121.js new file mode 100644 index 0000000..11e46f5 --- /dev/null +++ b/week7/exercise/task2-firstpart-251121.js @@ -0,0 +1,49 @@ +const http = require('http'); +const path = require('path'); +const fs = require('fs'); + +const pagesDirectory = path.join(__dirname, 'pages'); + +const PORT = 8000; + +const localJSONObject = { + testProp: 'testPropValue' +}; + +const handleNotFound = (res) => { + const notFoundFilePath = path.join(pagesDirectory, 'not-found.html'); + const readStream = fs.createReadStream(notFoundFilePath); + readStream.on('ready', () => { + res.writeHead(200, {'Content-Type': 'text/html'}); + }); + readStream.on('error', (err) => console.log(err)); + readStream.pipe(res); +}; + +const routes = { + 'notfound': handleNotFound +}; + +const server = http.createServer((req, res) => { + const url = req.url; + const urlExpectedExpr = /load\/:([0-9a-zA-Z-]*)/; + const propValue = url.match(urlExpectedExpr); + + // const propValuePostReq = url.match(urlExprForPostRequest); + + console.log(propValue); + // console.log(hostname); + + if (propValue === null || localJSONObject[propValue[1]] === undefined) { + // res.writeHead(301, {'Location': 'notfound'}); + handleNotFound(res); + return; + } + + const returnValue = localJSONObject[propValue[1]]; + + res.write(returnValue); + res.end(); +}); + +server.listen(PORT, () => { console.log(`Server is listening on ${PORT}...`) }); \ No newline at end of file From 3b59430916cd8a82ccb2a961fbae5be25a4d3ed4 Mon Sep 17 00:00:00 2001 From: Vladi Markova Date: Wed, 1 Dec 2021 19:22:41 +0200 Subject: [PATCH 2/2] add task1.1 1.2 solution --- .../exercise/task1.1.2_81271_week8_011221.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 week8/exercise/task1.1.2_81271_week8_011221.js diff --git a/week8/exercise/task1.1.2_81271_week8_011221.js b/week8/exercise/task1.1.2_81271_week8_011221.js new file mode 100644 index 0000000..840b006 --- /dev/null +++ b/week8/exercise/task1.1.2_81271_week8_011221.js @@ -0,0 +1,30 @@ +const express = require('express'); +const app = express(); + +const PORT = process.env.PORT || 3000; + +const events = [{ id: 1, name: 'Ev1', capacity: 50 }, { id: 2, name: 'Ev2', capacity: 50 }, { id: 3, name: 'Ev3', capacity: 50 }]; + +app.use(express.urlencoded()); + +app.post('/event', (req, res) => { + const {name, capacity} = req.body; + const event = { + id: events.length + 1, + name, + capacity + }; + events.push(event); + res.send(event); +}); + +app.get('/event/:id', (req, res) => { + const id = req.params.id; + console.log(id); + const event = events.filter((ev) => ev.id === parseInt(id)); + console.log(event); + res.send(event); +}) + + +app.listen(PORT, () => console.log(`Server is listening on PORT ${PORT}...`)); \ No newline at end of file