-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
58 lines (44 loc) · 2.28 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
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const multiparty = require('connect-multiparty');
const session = require('express-session');
const HomeController = require('./controllers/home');
const LoginController = require('./controllers/login');
const UsuarioController = require('./controllers/usuario');
const ChatController = require('./controllers/chat');
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(session({
secret: 'jedi',
resave: false,
saveUninitialized: false
}));
const authenticatePage = (req, res, next) => {
if (req.session.user) next();
else res.redirect('/');
};
const authenticateRequest = (req, res, next) => {
if (req.session.user) next();
else res.json([]);
};
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/', (req, res) => HomeController.home(req, res));
app.post('/login', (req, res) => LoginController.login(req, res));
app.get('/logout', (req, res) => LoginController.logout(req, res));
app.post('/cadastrar', multiparty(), (req, res) => UsuarioController.create(req, res));
app.get('/solicitacoes', authenticateRequest, (req, res) => UsuarioController.solicitacoes(req, res));
app.get('/amigos', authenticateRequest, (req, res) => UsuarioController.amigos(req, res));
app.get('/pessoas', authenticateRequest, (req, res) => UsuarioController.pessoas(req, res));
app.get('/postagens', authenticateRequest, (req, res) => UsuarioController.postagens(req, res));
app.post('/postagens', authenticateRequest, (req, res) => UsuarioController.createPostagem(req, res));
app.get('/solicitar/:id', authenticateRequest, (req, res) => UsuarioController.solicitar(req, res));
app.get('/aceitar/:id', authenticateRequest, (req, res) => UsuarioController.aceitar(req, res));
app.get('/conversar/:id', authenticatePage, (req, res) => ChatController.chat(req, res));
app.post('/mensagens/:id', authenticateRequest, (req, res) => ChatController.addMensagem(req, res));
app.get('/mensagens/:id', authenticateRequest, (req, res) => ChatController.mensagens(req, res));
app.listen(process.env.PORT || 3000, () => console.log('Aplicação funcionando!'));