-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (58 loc) · 1.81 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
require("dotenv").config()
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const cors = require('cors');
mongoose.connect(process.env.CONNECTION_STR).then(() => {
app.emit('conectado')
console.log("Banco de dados conectado com sucesso!.. Iniciando app")
}).catch(err => {
console.log(err)
})
const Session = require("express-session");
const MemoryStore = require('memorystore')(Session);
const flash = require('express-flash');
const PORT = process.env.PORT || 3001;
const routes = require('./routes');
const path = require('path');
const helmet = require('helmet');
const csrf = require('csurf');
const { middlewareGlobal, checksCsrfError, csrfMiddleware } = require('./src/middlewares/middleware');
app.use(helmet())
// Habilitar o middleware cors para todas as rotas
app.use(cors());
app.use(express.json())
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.resolve(__dirname, 'public')));
app.use((req, res, next) => {
res.setHeader('Content-Security-Policy', "script-src 'self' cdn.jsdelivr.net;");
next();
});
const sessionOptions = Session({
secret: "kkkkkaaaaaaaaaa",
store: new MemoryStore({
checkPeriod: 86400000 // Tempo em milissegundos para verificar a expiração das sessões
}),
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24,
httpOnly: true
}
})
app.use(sessionOptions);
app.use(flash())
app.set('views', path.resolve(__dirname, 'src', 'views'));
app.set('view engine', 'ejs');
app.use(csrf())
app.use(middlewareGlobal);
app.use(checksCsrfError)
app.use(csrfMiddleware)
app.use(routes);
app.on('conectado', () => {
app.listen(PORT, () => {
console.log('Servidor Iniciado com sucesso');
console.log('Acessar http://localhost:3000')
})
})
module.exports = app;