-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
121 lines (98 loc) · 2.97 KB
/
server.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
const express = require("express");
const server = express();
const db = require("./db")
//configurar arquivos estáticos
server.use(express.static(__dirname + "/ws/public"))
//habilitar uso do req.body
server.use(express.urlencoded({ extended: true }))
//const ideias = [
// {
// img: "https://image.flaticon.com/icons/svg/3021/3021831.svg",
// title: "Flores no vaso",
// category: "Flores",
// description: "Lorem ipsum dolor, sit amet, adhuc nulla definiebas mei ad, ei doming aperiam delicata est.",
// url: "https://www.linkedin.com/in/nathan-a-1b9436124/"
// },
// {
// img: "https://image.flaticon.com/icons/png/512/1198/1198654.png",
// title: "Plantas no vaso",
// category: "Plantas",
// description: "Lorem ipsum dolor, sit amet, adhuc nulla definiebas mei ad, ei doming aperiam delicata est.",
// url: "https://www.linkedin.com/in/nathan-a-1b9436124/"
// },
// {
// img: "https://image.flaticon.com/icons/svg/3057/3057195.svg",
// title: "Mini-Hortas",
// category: "Hortaliças",
// description: "Lorem ipsum dolor, sit amet, adhuc nulla definiebas mei ad, ei doming aperiam delicata est.",
// url: "https://www.linkedin.com/in/nathan-a-1b9436124/"
// },
// {
// img: "https://image.flaticon.com/icons/png/512/807/807421.png",
// title: "Tipos de vasos",
// category: "Vasos",
// description: "Lorem ipsum dolor, sit amet, adhuc nulla definiebas mei ad, ei doming aperiam delicata est.",
// url: "https://www.linkedin.com/in/nathan-a-1b9436124/"
// },
//]
//configuração do nunjucks
const nunjucks = require("nunjucks");
nunjucks.configure(__dirname + "/ws/views", {
express: server,
noCache: true,
})
//rotas
server.get("/", function(req, res){
db.all(`SELECT * FROM ideias`, function(err, rows){
if (err) {
console.log(err)
return res.send("Erro no banco de dados")
}
const reversedIdeias = [...rows].reverse()
let lastIdeias = []
for (let ideia of reversedIdeias){
if (lastIdeias.length < 2){
lastIdeias.push(ideia)
}
}
return res.render("index.html", {ideias: lastIdeias})
})
})
server.get("/ideias", function(req, res){
db.all(`SELECT * FROM ideias`, function(err, rows){
if (err) {
console.log(err)
return res.send("Erro no banco de dados")
}
const reversedIdeias = [...rows].reverse()
return res.render("ideias.html", {ideias: reversedIdeias})
})
})
server.post("/", function(req, res){
//Inserir dados
const query = `
INSERT INTO ideias(
image,
title,
category,
description,
link
) VALUES(?,?,?,?,?);
`
const values = [
req.body.image,
req.body.title,
req.body.category,
req.body.description,
req.body.link
]
db.run(query, values, function(err){
if (err) {
console.log(err)
return res.send("Erro no banco de dados")
}
return res.redirect("/ideias")
})
})
//servidor na porta 3333
server.listen(3333)