Skip to content
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
19 changes: 18 additions & 1 deletion app/public/zadanie01/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
<!-- Twój kod -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Coders Lab</title>
</head>
<body>
<h1>Czy B jest dzielnikiem A?</h1>
<form action="/result" method="post">
Liczba A: <input type="text" name="a"><br>
Liczba B: <input type="text" name="b"><br>
<button type="submit">Sprawdź!</button>
</form>
</body>
</html>
19 changes: 18 additions & 1 deletion app/public/zadanie02/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
<!-- Twój kod -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Coders Lab</title>
</head>
<body>
<form action="/cookie/set" method="post">
Imię: <input type="text" name="name"><br>
<button type="submit">Zapisz</button>
</form>
<a href="/cookie/show">Pokaż imię</a>
<a href="/cookie/check">Sprawdź imię</a>
</body>
</html>
16 changes: 16 additions & 0 deletions app/public/zadanieDnia/add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Add comment</title>
</head>
<body>
<form action="/save" method="post">
<textarea name="comment"></textarea><br>
<button type="submit">Dodaj komentarz</button>
</form>
</body>
</html>
1 change: 0 additions & 1 deletion app/public/zadanieDnia/index.html

This file was deleted.

18 changes: 17 additions & 1 deletion app/zadanie01.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
//Twój kod
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Świetnie :)

}));
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');
});
30 changes: 29 additions & 1 deletion app/zadanie02.js
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
//Twój kod
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');
});
55 changes: 54 additions & 1 deletion app/zadanieDnia.js
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
//Twój kod
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 + '<hr><a href="/add.html">Dodaj nowy komentarz</a>');
});

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. <a href="/">Zobacz komentarze</a>');
});

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) : [];
}