-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
75 lines (59 loc) · 1.83 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
const express = require('express')
const exphbs = require('express-handlebars')
const session = require('express-session')
const flash = require('express-flash')
//salvar sessoes em arquivos no servidor
const FileStore = require('session-file-store')(session)
require('dotenv').config()
const app = express()
const connection = require('./db/connection')
//models
const Tought = require('./models/Tought')
const User = require('./models/User')
//routes
const toughtRoutes = require('./routes/toughtsRouters')
const authRoutes = require('./routes/authRoutes')
const ToughtController = require('./controllers/ToughtController') // para acessar a /
//template engine
app.engine('handlebars', exphbs.engine())
app.set('view engine', 'handlebars')
//body parser, necessario para se trabalhar com forms
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(express.static('public'))
//session middleware
app.use(session({
name: 'session',
secret: 'segredo_segredinho',
resave: false,
saveUninitialized: false,
store: new FileStore({
logFn: function() {},
path: require('path').join(require('os').tmpdir(),'sessions') //diretorio que serao salvos as sessoes dos usuarios
}),
cookie: {
secure: false,
maxAge: 360000,
expires: new Date(Date.now()+360000),
httpOnly: true //em producao trocar para https
}
}))
// flash messages
app.use(flash())
//garantir que tenhamos o userid a cada request
app.use((req, res, next) =>{
if(req.session.userid){
res.locals.session = req.session
}
next()
})
//Routes
app.use('/toughts', toughtRoutes)
app.use('/', authRoutes)
app.get('/', ToughtController.showToughts)
//connection.sync({force: true})
connection.sync()
.then(() =>{
app.listen(3000)
})
.catch((err) => {console.log(err)})