-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 loc) · 1.46 KB
/
index.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
var express = require("express");
var exphbs = require("express-handlebars");
var session = require("express-session");
var bodyParser = require("body-parser");
var urlencodedParser = bodyParser.urlencoded({ extended: false });
const defaultNote = {
title: "Willkommen",
content:
"Herzlich wilkommen bei NeverNote, der sichersten Notizen-App der Welt. ",
};
let publicNotes = [defaultNote];
var app = express();
app.use(urlencodedParser);
app.set("trust proxy", 1); // trust first proxy
app.use(
session({
secret: "random very secure text",
resave: false,
maxAge: 24*60*60*1000,
saveUninitialized: true,
cookie: { secure: false },
})
);
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
app.use("/static", express.static("static"));
app.get("/", function (req, res) {
const privateNotes = req.session.notes || [];
res.render("home", { publicNotes, privateNotes });
});
app.post("/", function (req, res) {
const note = {
title: req.body.title,
content: req.body.content,
};
if (req.body.public) {
publicNotes.unshift(note);
} else {
if(req.session.notes) req.session.notes.unshift(note);
else req.session.notes = [note];
}
res.redirect('/')
});
app.post("/clear", function (req, res) {
req.session.notes = [];
res.redirect('/')
});
app.post("/clearall", function (req, res) {
console.log("delete everything");
publicNotes = [defaultNote];
});
app.listen(3000);