74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
const express = require("express")
|
|
const app = express()
|
|
const PORT = 3000
|
|
|
|
app.use(express.urlencoded({
|
|
extended: true
|
|
}));
|
|
|
|
app.get("/", function (req, res) {
|
|
let samochody = ['audi', 'opel', 'dyliżans', 'tramwaj', 'ford (1.0 ecoboost xd)', 'elektryczny smart']
|
|
|
|
html =
|
|
`
|
|
<html lang="pl">
|
|
<head>
|
|
<title>Samochody</title>
|
|
<link rel="stylesheet" type="text/css" href="css/style.css">
|
|
</head>
|
|
|
|
<body>
|
|
<form action="/handle_form" method="post">
|
|
<table>
|
|
<thead>
|
|
<th></th>
|
|
<th></th>
|
|
<th>nowych</th>
|
|
<th>używanych</th>
|
|
<th>powypadkowych</th>
|
|
</thead>
|
|
`
|
|
|
|
for (let i = 0; i < samochody.length; i++) {
|
|
html += `<tr>
|
|
<td>${i + 1}</td>
|
|
<td>${samochody[i]}</td>
|
|
<td><input type="radio" name="stan-${samochody[i]}" value="nowy"></td>
|
|
<td><input type="radio" name="stan-${samochody[i]}" value="uzywany"></td>
|
|
<td><input type="radio" name="stan-${samochody[i]}" value="powypadkowy"></td>
|
|
</tr>`
|
|
}
|
|
|
|
html += '</table><input type="submit" value="Wyślij"></form></body></html>'
|
|
|
|
res.send(html)
|
|
})
|
|
|
|
app.post("/handle_form", function (req, res) {
|
|
let stany_aut = {
|
|
"nowe": 0,
|
|
"używane": 0,
|
|
"powypadkowe": 0
|
|
}
|
|
for (let i = 0; i < Object.keys(req.body).length; i++) {
|
|
switch (Object.values(req.body)[i]) {
|
|
case "nowy":
|
|
stany_aut.nowe += 1
|
|
break
|
|
case "uzywany":
|
|
stany_aut.używane += 1
|
|
break
|
|
case "powypadkowy":
|
|
stany_aut.powypadkowe += 1
|
|
break
|
|
}
|
|
}
|
|
res.send(stany_aut)
|
|
})
|
|
|
|
app.use(express.static('static'))
|
|
|
|
app.listen(PORT, function () {
|
|
console.log("Serwer uruchomiony na porcie " + PORT)
|
|
})
|