-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (64 loc) · 1.71 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
const express = require('express')
const nunjucks = require('nunjucks')
const api = express()
const db = require('./src/database/db')
api.use(express.static('public'))
//habilitar o uso do req.body
api.use(express.urlencoded({ extended: true }))
nunjucks.configure('src', {
express: api,
noCache: true
})
api.get('/', (req, res) => {
res.render('index.html')
})
api.get('/create-point', (req,res) => {
console.log(req.query)
res.render('create-point.html')
})
api.post('/create-point', (req, res) => {
const query = `INSERT INTO places(
image,
name,
address,
address2,
state,
city,
items
) VALUES (?,?,?,?,?,?,?);`
const values = [
req.body.image,
req.body.name,
req.body.address,
req.body.address2,
req.body.state,
req.body.city,
req.body.items
]
function afterInsertData(err){
if(err){
console.log(err)
return res.send('Erro no cadastro')
}
console.log('Cadastrado com sucesso')
console.log(this)
return res.render('create-point.html', { saved: true })
}
db.run(query, values, afterInsertData)
})
api.get('/search', (req,res) => {
const search = req.query.search
if(search == ''){
return res.render('search-results.html', { total: 0 })
}
db.all(`SELECT * FROM places WHERE city LIKE '%${search}%'`, function(err, rows){
if(err){
return console.log(err)
}
const total = rows.length
return res.render('search-results.html', { places: rows, total })
})
})
api.listen('3000', (req, res) =>{
console.log('It is running!')
})