From c93ca753f37590081087a0e0a80a85567eb615d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Woz=CC=81niak?= Date: Mon, 5 Feb 2018 15:39:37 +0100 Subject: [PATCH] fun with js #6 --- app/public/zadanie01/index.html | 26 ++++++++++++++++++- app/public/zadanie02/index.html | 22 ++++++++++++++++- app/public/zadanieDnia/add.html | 18 ++++++++++++++ app/zadanie01.js | 16 +++++++++++- app/zadanie02.js | 44 ++++++++++++++++++++++++++++++++- app/zadanieDnia.js | 42 ++++++++++++++++++++++++++++++- 6 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 app/public/zadanieDnia/add.html diff --git a/app/public/zadanie01/index.html b/app/public/zadanie01/index.html index c116dfd..a47f158 100644 --- a/app/public/zadanie01/index.html +++ b/app/public/zadanie01/index.html @@ -1 +1,25 @@ - \ No newline at end of file + + + + + + + Coders Lab + + + +
+ + + +
+ + + \ No newline at end of file diff --git a/app/public/zadanie02/index.html b/app/public/zadanie02/index.html index c116dfd..96b4072 100644 --- a/app/public/zadanie02/index.html +++ b/app/public/zadanie02/index.html @@ -1 +1,21 @@ - \ No newline at end of file + + + + + + + Coders Lab + + + +
+ + +
+ + + \ No newline at end of file diff --git a/app/public/zadanieDnia/add.html b/app/public/zadanieDnia/add.html new file mode 100644 index 0000000..dc8d36f --- /dev/null +++ b/app/public/zadanieDnia/add.html @@ -0,0 +1,18 @@ + + + + + Add post + + + + +
+ + +
+ + + \ No newline at end of file diff --git a/app/zadanie01.js b/app/zadanie01.js index 8c20173..b4c570d 100644 --- a/app/zadanie01.js +++ b/app/zadanie01.js @@ -1 +1,15 @@ -//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()); +app.use(express.static('./public/zadanie01/')); + + +app.post("/divider", (req, resp) => { + resp.send( (parseInt(req.body.numberA) % parseInt(req.body.numberB) === 0) ? "Liczba B jest dzielnikiem liczby A" : "Liczba B nie jest dzielnikiem liczby A" ); +}); + +app.listen(3000); \ No newline at end of file diff --git a/app/zadanie02.js b/app/zadanie02.js index 8c20173..3f38b08 100644 --- a/app/zadanie02.js +++ b/app/zadanie02.js @@ -1 +1,43 @@ -//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()); +app.use(express.static('./public/zadanie02/')); + + +app.post("/cookie/set", (req, res) => { + if (req.body.name) { + res.cookie('name', req.body.name, { + maxAge: 1000 * 60 * 60 * 24 * 30, + }); + res.send('Ciastko ustawione!'); + } + else { + res.send('Nie podałeś imienia!'); + } +}); + +app.get("/cookie/show", (req, res) => { + const name = req.cookies.name; + if (typeof name !== 'undefined') { + res.send("Imię zapisane w ciastku to: " + name); + } + else { + res.send("Nie ma zapisanego imienia w ciastku"); + } +}); + +app.get("/cookie/check", (req, res) => { + const name = req.cookies.name; + if (typeof name !== 'undefined') { + res.send("Imię jest zapisane w ciastku"); + } + else { + res.send("Nie ma zapisanego imienia w ciastku"); + } +}); + +app.listen(3000); \ No newline at end of file diff --git a/app/zadanieDnia.js b/app/zadanieDnia.js index 8c20173..dbebb6f 100644 --- a/app/zadanieDnia.js +++ b/app/zadanieDnia.js @@ -1 +1,41 @@ -//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()); +app.use(express.static('./public/zadanieDnia/')); + + +let posts = []; + +app.get("/", (req, res) => { + let response = ''; + + if (req.cookies.posts) { + posts = JSON.parse(req.cookies.posts); + } + + if (posts.length > 0) { + posts.forEach( val => { + response += "post: " + val + "
"; + }); + } + + response += "
Dodaj posta"; + res.send(response); +}); + +app.post("/save", (req, res) => { + if (req.cookies.posts) { + posts = JSON.parse(req.cookies.posts); + } + if (req.body.post) { + posts.push(req.body.post); + res.cookie("posts", JSON.stringify(posts)); + } + res.send("Zapisano
Strona główna") +}); + +app.listen(3000); \ No newline at end of file