diff --git a/app/public/zadanie01/index.html b/app/public/zadanie01/index.html index c116dfd..32f01bf 100644 --- a/app/public/zadanie01/index.html +++ b/app/public/zadanie01/index.html @@ -1 +1,18 @@ - \ No newline at end of file + + + + + + + Coders Lab + + +

Czy B jest dzielnikiem A?

+
+ Liczba A:
+ Liczba B:
+ +
+ + diff --git a/app/public/zadanie02/index.html b/app/public/zadanie02/index.html index c116dfd..983c088 100644 --- a/app/public/zadanie02/index.html +++ b/app/public/zadanie02/index.html @@ -1 +1,18 @@ - \ No newline at end of file + + + + + + + Coders Lab + + +
+ Imię:
+ +
+ Pokaż imię + Sprawdź imię + + diff --git a/app/public/zadanieDnia/add.html b/app/public/zadanieDnia/add.html new file mode 100644 index 0000000..cca2eab --- /dev/null +++ b/app/public/zadanieDnia/add.html @@ -0,0 +1,16 @@ + + + + + + + Add comment + + +
+
+ +
+ + diff --git a/app/public/zadanieDnia/index.html b/app/public/zadanieDnia/index.html deleted file mode 100644 index c116dfd..0000000 --- a/app/public/zadanieDnia/index.html +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/app/zadanie01.js b/app/zadanie01.js index 8c20173..d463d20 100644 --- a/app/zadanie01.js +++ b/app/zadanie01.js @@ -1 +1,17 @@ -//Twój kod \ No newline at end of file +const express = require('express'); +const bodyParser = require('body-parser'); +const app = express(); +app.use(bodyParser.urlencoded({ + extended: true //body-parser deprecated undefined extended: provide extended option +})); +app.use(express.static('./public/zadanie01/')); + +app.post('/result', (req, res) => { + const {a, b} = req.body; + const is = parseInt(a) % parseInt(b) === 0 ? '' : ' nie'; + res.send('Liczba ' + b + is + ' jest dzielnikiem liczby ' + a); +}); + +app.listen(3000, () => { + console.log('Serwer uruchomiony na porcie 3000'); +}); diff --git a/app/zadanie02.js b/app/zadanie02.js index 8c20173..4aab2bc 100644 --- a/app/zadanie02.js +++ b/app/zadanie02.js @@ -1 +1,29 @@ -//Twój kod \ No newline at end of file +const express = require('express'); +const cookieParser = require('cookie-parser'); +const bodyParser = require('body-parser'); +const app = express(); +app.use(cookieParser()); +app.use(bodyParser.urlencoded({ + extended: true +})); +app.use(express.static('./public/zadanie02/')); + +app.post('/cookie/set', (req, res) => { + res.cookie('cookieName', req.body.name, { + maxAge : 2628000000, + }); + res.send('Zapisano imię: ' + req.body.name); +}); + +app.get('/cookie/show', (req, res) => { + res.send('Zapisane imię: ' + req.cookies.cookieName); +}); + +app.get('/cookie/check', (req, res) => { + const is = req.cookies.cookieName === undefined ? 'nie' : ''; + res.send('Imię '+ is + ' zostało zapisane w ciastku.'); +}); + +app.listen(3000, () => { + console.log('Serwer uruchomiony na porcie 3000'); +}); diff --git a/app/zadanieDnia.js b/app/zadanieDnia.js index 8c20173..67ee643 100644 --- a/app/zadanieDnia.js +++ b/app/zadanieDnia.js @@ -1 +1,54 @@ -//Twój kod \ No newline at end of file +const express = require('express'); +const cookieParser = require('cookie-parser'); +const bodyParser = require('body-parser'); +const app = express(); +app.use(cookieParser()); +app.use(bodyParser.urlencoded({ + extended: true +})); +app.use(express.static('./public/zadanieDnia/')); + +app.get('/remove', (req, res) => { + res.clearCookie('comments'); + res.send('Komentarze usunięte!'); +}); + +app.get('/', (req, res) => { + const comments = readComments(req.cookies.comments); + res.send(comments + '
Dodaj nowy komentarz'); +}); + +app.post('/save', (req, res) => { + const comments = addComment(req.cookies.comments, req.body.comment); + res.cookie('comments', comments, { + maxAge: 2628000000, + }); + res.send('Zapisano nowy komentarz. Zobacz komentarze'); +}); + +app.listen(3000, () => { + console.log('Serwer uruchomiony na porcie 3000'); +}); + +// Funkcje pomocnicze + +/** + * Ta funkcja pobiera string dotychczasowego ciastka, dodaje nowy komentarz i zwraca nowy string - taki z jakim należy nadpisać to ciasto. + * @param {string} commentsCookieValue Wartość dotychczasowego ciastka przechowującego komentarze + * @param {string} newComment Nowy komentarz + * @return {string} Nowy string z komentarzami do zapisania w ciastku + */ +function addComment(commentsCookieValue, newComment) { + const comments = readComments(commentsCookieValue); + comments.push(newComment); + return JSON.stringify(comments); +} + +/** + * Ta funkcja odczytuje już dodane komentarze i zwraca je w postaci tablicy. + * @param {string} commentsCookieValue Wartość dotychczasowego ciastka przechowującego komentarze + * @return {Array} Tablica z komentarzami + */ +function readComments(commentsCookieValue) { + return commentsCookieValue ? JSON.parse(commentsCookieValue) : []; +}