-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (115 loc) · 3.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// pridanie serveru express
// @link: http://expressjs.com/
var express = require('express');
var bodyParser = require('body-parser');
// pridanie template enginu handlebars
// @link: https://github.com/ericf/express-handlebars
var express_handlebars = require('express-handlebars');
var session = require('express-session');
// pridanie a nastavenie knex databázového wrapperu
// @link: http://knexjs.org/
var knex = require('knex')({
client: 'mysql',
connection: {
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'node'
}
});
// vytvorenie express inštancie
var app = express();
// pridanie handlebars ako view enginu pre express
app.engine('handlebars', express_handlebars({defaultLayout: 'main'}));
// nastavenie defaultného view enginu na handlebars
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({extended: false}));
app.use(session({
secret: 'some secret!', // important to stay secret!
resave: false,
saveUninitialized: true
}));
app.use(function (req, res, next) {
console.log(req.session);
req.isAuthentificated = function () {
return !!req.session.user;
};
req.session.user = {
name: 'Test Testerson'
};
next();
});
// pridanie middleware, vždy sa vykoná predtým než sa vykoná nasledujúce
app.use(function (req, res, next) {
console.log(req.url);
// spustí vykonanie nasledujúceho
next();
});
// routa na /, teda hlavnú stránku
app.get('/', function (req, res) {
knex.select('*').from('books').then(function (results) {
res.render('pages/home', {
books: results
});
//res.json(results);
}).catch(function (error) {
console.log(error);
});
});
app.post('/login', function (req, res) {
if (
req.body.name === 'test' &&
req.body.password === 'password'
) {
req.session.user = {
name: 'Test Testerson'
};
res.redirect('/');
} else {
res.send('Wrong username/password.');
}
});
app.get('/login', function (req, res) {
res.render('pages/login');
});
// routa na book/:id
app.post('/book/add', function (req, res) {
var title = req.body.title;
var description = req.body.description;
knex('books').insert({title: title, description: description}).then(function (result) {
res.redirect("/");
}).catch(function (error) {
console.log(error);
});
});
// routa na book/:id
app.get('/book/:id', function (req, res) {
var id = req.params.id;
knex.select('*').from('books').where('id', id).then(function (results) {
res.render('pages/book', {
books: results
});
//res.json(results);
}).catch(function (error) {
console.log(error);
});
});
// routa na book/:id/delete
app.get('/book/:id/delete', function (req, res) {
var id = req.params.id;
knex.delete().from('books').where('id', id).then(function (results) {
res.redirect('/');
}).catch(function (error) {
console.log(error);
});
});
// všetky predtým nehachytené routy, vhodné pre 404
app.get('*', function (req, res) {
res.json(req.url);
});
// spustenie serveru na danom porte
var port = 8000;
app.listen(port, function () {
console.log('App is listening on port ' + port + '!');
});
// v konzoli zapnúť pomocou node 'app.js', alebo 'nodemon app.js'