-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (59 loc) · 1.54 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
66
67
68
69
70
71
72
73
74
75
76
const express = require('express')
const dotenv = require('dotenv').config()
const DB_USER = process.env.DB_USER
const DB_PASS = process.env.DB_PASS
const exphbs = require('express-handlebars')
const port = 3000
const mysql = require('mysql')
//const path = require('path')
//const basePath = path.join(__dirname, 'templates')
//const userRoutes = require('./users')
const app = express()
app.engine('handlebars', exphbs.engine())
app.set('view engine', 'handlebars')
//criando um middleware
// middleware possui função anônima com 3 params
const checkAuth = function(req, res, next) {
req.authStatus = true
if (req.authStatus) {
console.log('está logado!')
next()
} else {
console.log('não está logado!')
next()
}
}
//ler o body da requisição
app.use(
express.urlencoded({
extended: true
})
)
app.use(express.json())
app.use(express.static('public'))
// usando módulo externo de rotas
//app.use('/users', userRoutes)
app.get('/', (req, res) => {
//res.send('olá mundo!')
//res.sendFile(`${basePath}/index.html`)
res.render('home')
})
const conn = mysql.createConnection({
host: 'localhost',
user: DB_USER,
password: DB_PASS,
dabatase: 'node-mysql0'
})
conn.connect(function(err) {
if (err) {
console.log(err)
} else {
console.log('conectou ao mysql!')
}
app.listen(port, () => {
console.log(`listening to port ${port}`)
})
})
app.use(function (req, res, next) {
res.status(404).sendFile(`${basePath}/404.html`)
})