Lekcja 08 projekt auta cz1
This commit is contained in:
1 parent
8fc98ebf9d
commit
d60f8a91a7
1543 files changed
+255781
No files matched your search
@@ -0,0 +1,86 @@
|
||||
const DS = require('@seald-io/nedb');
|
||||
const express = require('express');
|
||||
const { create } = require('express-handlebars');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
const DB = new DS({
|
||||
filename: 'data/cars.db',
|
||||
autoload: true
|
||||
});
|
||||
|
||||
const hbs = create({
|
||||
defaultLayout: 'main.hbs',
|
||||
extname: '.hbs'
|
||||
});
|
||||
|
||||
app.engine('.hbs', hbs.engine);
|
||||
app.set('view engine', '.hbs');
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
|
||||
const model = [
|
||||
"Ford Fiesta",
|
||||
"Fiat Siesta",
|
||||
"Volkswagen Polo",
|
||||
"Volvo V70",
|
||||
"Volvo S60",
|
||||
"Newag Impuls"
|
||||
];
|
||||
|
||||
/// routingi
|
||||
|
||||
app.get("/", function (req, res) {
|
||||
res.render('index.hbs');
|
||||
});
|
||||
|
||||
app.post("/add", function (req, res) {
|
||||
let doc = {
|
||||
ubezpieczony: req.body.ubezpieczony ? "TAK" : "NIE",
|
||||
benzyna: req.body.benzyna ? "TAK" : "NIE",
|
||||
uszkodzony: req.body.uszkodzony ? "TAK" : "NIE",
|
||||
naped4x4: req.body.naped4x4 ? "TAK" : "NIE",
|
||||
model: req.body.model
|
||||
};
|
||||
|
||||
DB.insert(doc, function (err, new_doc) {
|
||||
// ok
|
||||
res.redirect(`/add?model=${doc.model}&id=${new_doc._id}`);
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/add", function (req, res) {
|
||||
let r_model = model[Math.floor(Math.random() * model.length)]
|
||||
|
||||
let ctx = {
|
||||
"model": r_model
|
||||
};
|
||||
|
||||
if (req.query.model && req.query.id) {
|
||||
ctx.added_model = req.query.model
|
||||
ctx.added_id = req.query.id
|
||||
}
|
||||
|
||||
res.render("add.hbs", ctx);
|
||||
});
|
||||
|
||||
app.get("/list", function (req, res) {
|
||||
DB.find({}, function (err, docs) {
|
||||
let ctx = {
|
||||
car: docs
|
||||
};
|
||||
res.render('list.hbs', ctx);
|
||||
})
|
||||
});
|
||||
|
||||
/// --------
|
||||
|
||||
app.use(express.static('static'))
|
||||
|
||||
app.listen(PORT, function () {
|
||||
console.log(`Listening on ${PORT}`)
|
||||
})
|
||||
Reference in new issue
Block a user