66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
const context = {
|
|
subject: "ćwiczenie 4 - dane z tablicy, select",
|
|
fields: [
|
|
{ name: "title" },
|
|
{ name: "author" },
|
|
{ name: "lang" }
|
|
],
|
|
books: [
|
|
{ title: "Lalka", author: "B Prus", lang: "PL" },
|
|
{ title: "Hamlet", author: "W Szekspir", lang: "ENG" },
|
|
{ title: "Pan Wołodyjowski", author: "H Sienkiewicz", lang: "PL" },
|
|
{ title: "Zamek", author: "F Kafka", lang: "CZ" }
|
|
]
|
|
}
|
|
|
|
const express = require("express")
|
|
const app = express()
|
|
const PORT = 3000
|
|
const path = require('path')
|
|
|
|
const { create } = require('express-handlebars');
|
|
|
|
const hbs = create({
|
|
defaultLayout: 'main.hbs', // Domyślny layout: views/layouts/main.hbs
|
|
extname: '.hbs' // rozszerzenie plików szablonów
|
|
});
|
|
|
|
app.engine('.hbs', hbs.engine); // określenie silnika szablonów
|
|
app.set('view engine', '.hbs');
|
|
app.set('views', path.join(__dirname, 'views')); // ustalamy katalog views
|
|
|
|
app.get("/", function (req, res) {
|
|
res.render('index04.hbs', context)
|
|
})
|
|
|
|
app.get("/handle04", function (req, res) {
|
|
let data = {
|
|
subject: context.subject,
|
|
books: []
|
|
};
|
|
|
|
if (!req.query.field) {
|
|
data.books.push({ value: "Musisz wybrać co najmniej jedno kryterium!" })
|
|
} else {
|
|
switch (req.query.field) {
|
|
case 'title':
|
|
context.books.forEach(book => { data.books.push({ value: book.title }) })
|
|
break
|
|
case 'author':
|
|
context.books.forEach(book => { data.books.push({ value: book.author }) })
|
|
break
|
|
case 'lang':
|
|
context.books.forEach(book => { data.books.push({ value: book.lang }) })
|
|
break
|
|
}
|
|
}
|
|
|
|
res.render('index041.hbs', data);
|
|
})
|
|
|
|
app.use(express.static("static"))
|
|
|
|
app.listen(PORT, function () {
|
|
console.log("start serwera na porcie " + PORT)
|
|
})
|