Skip to content

Commit ff7e315

Browse files
author
tamara
committed
azure cloud primer
1 parent b36a5b5 commit ff7e315

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

8-azure/data/wishlist.json

Whitespace-only changes.

8-azure/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "docker-complete",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"author": "Aleksa Miletić",
7+
"license": "MIT",
8+
"dependencies": {
9+
"express": "^4.17.3",
10+
"body-parser": "1.19.0"
11+
}
12+
}

8-azure/public/styles.css

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
html {
2+
font-family: 'Arial', sans-serif;
3+
}
4+
5+
body {
6+
margin: 0;
7+
background-color: #f4f4f4;
8+
display: flex;
9+
align-items: center;
10+
justify-content: center;
11+
height: 100vh;
12+
}
13+
14+
section,
15+
form {
16+
padding: 2rem;
17+
border-radius: 8px;
18+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
19+
background-color: #ffffff;
20+
margin: 2rem;
21+
max-width: 30rem;
22+
}
23+
24+
.form-control {
25+
margin: 1rem 0;
26+
}
27+
28+
input {
29+
font: inherit;
30+
width: 100%;
31+
padding: 0.5rem;
32+
border: 1px solid #ccc;
33+
border-radius: 4px;
34+
}
35+
36+
input:focus {
37+
outline: none;
38+
border-color: #ff9800;
39+
}
40+
41+
input,
42+
label {
43+
display: block;
44+
}
45+
46+
label {
47+
font-weight: bold;
48+
margin-bottom: 0.5rem;
49+
color: #ff9800;
50+
}
51+
52+
button {
53+
background-color: #ff9800;
54+
border: none;
55+
color: white;
56+
cursor: pointer;
57+
padding: 0.75rem 1.5rem;
58+
border-radius: 4px;
59+
}
60+
61+
button:hover,
62+
button:active {
63+
background-color: #ffac33;
64+
}
65+
66+
table {
67+
width: 100%;
68+
border-collapse: collapse;
69+
margin-top: 1rem;
70+
}
71+
72+
th, td {
73+
padding: 0.5rem;
74+
border: 1px solid #ccc;
75+
text-align: left;
76+
}
77+
78+
th {
79+
background: #ff98001a;
80+
}

8-azure/server.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const app = express();
7+
8+
/* ---------- 1. Put do JSON-a i inicijalno učitavanje ---------- */
9+
const DATA_DIR = path.join(__dirname, 'data');
10+
const DATA_FILE = path.join(DATA_DIR, 'wishlist.json'); // …/data/wishlist.json
11+
12+
let wishList = [];
13+
try {
14+
// ako datoteka postoji – učitaj; inače kreni od praznog niza
15+
wishList = JSON.parse(fs.readFileSync(DATA_FILE, 'utf-8'));
16+
} catch (_) {
17+
wishList = [];
18+
}
19+
20+
/* ---------- 2. Middleware ---------- */
21+
app.use(bodyParser.urlencoded({ extended: false }));
22+
app.use(express.static('public'));
23+
24+
/* ---------- 3. GET / ---------- */
25+
app.get('/', (req, res) => {
26+
// kreiramo HTML redove za tabelu
27+
const rows = wishList
28+
.map(
29+
(item, idx) =>
30+
`<tr><td>${idx + 1}</td><td>${item}</td></tr>`
31+
)
32+
.join('');
33+
34+
res.send(`
35+
<!doctype html>
36+
<html lang="sr">
37+
<head>
38+
<meta charset="utf-8" />
39+
<title>Lista želja</title>
40+
<link rel="stylesheet" href="styles.css" />
41+
</head>
42+
<body>
43+
<section>
44+
<h2>Moja lista želja</h2>
45+
${
46+
wishList.length
47+
? `<table><thead><tr><th>#</th><th>Želja</th></tr></thead><tbody>${rows}</tbody></table>`
48+
: '<p>Još nema unetih želja.</p>'
49+
}
50+
</section>
51+
52+
<form action="/store-wishlist" method="POST">
53+
<div class="form-control">
54+
<label for="wishlistItem">Element liste:</label>
55+
<input id="wishlistItem" type="text" name="wishlistItem" required />
56+
</div>
57+
<button type="submit">Dodaj u listu</button>
58+
</form>
59+
</body>
60+
</html>
61+
`);
62+
});
63+
64+
/* ---------- 4. POST /store-wishlist ---------- */
65+
app.post('/store-wishlist', (req, res) => {
66+
const item = (req.body.wishlistItem || '').trim();
67+
if (!item) return res.redirect('/');
68+
69+
wishList.push(item);
70+
71+
// obezbedimo da /data postoji
72+
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR);
73+
74+
// asinhrono upisujemo izmene
75+
fs.writeFile(
76+
DATA_FILE,
77+
JSON.stringify(wishList, null, 2),
78+
(err) => err && console.error('Greška pri čuvanju JSON-a:', err)
79+
);
80+
81+
res.redirect('/'); // PRG obrazac
82+
});
83+
84+
app.listen(80, () => console.log('Server sluša na portu 80'));

0 commit comments

Comments
 (0)