Skip to content

add solution to first part of task2 of 251121 exercise #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions week7/exercise/task2-firstpart-251121.js
Original file line number Diff line number Diff line change
@@ -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}...`) });
30 changes: 30 additions & 0 deletions week8/exercise/task1.1.2_81271_week8_011221.js
Original file line number Diff line number Diff line change
@@ -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}...`));