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 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