forked from mtov/esmforum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (59 loc) · 1.48 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
const express = require('express')
const modelo = require('./modelo.js');
const app = express()
app.use(express.urlencoded({ extended: true }))
app.use(express.static('public'));
app.set('views', './visao');
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
try {
const perguntas = modelo.listar_perguntas();
res.render('index', {
perguntas: perguntas
});
}
catch(erro) {
res.status(500).json(erro.message);
}
});
app.post('/perguntas', (req, res) => {
try {
modelo.cadastrar_pergunta(req.body.pergunta);
res.render('pergunta-sucesso');
}
catch(erro) {
res.status(500).json(erro.message);
}
});
app.get('/respostas', (req, res) => {
const id_pergunta = req.query.id_pergunta;
const pergunta = modelo.get_pergunta(id_pergunta);
const respostas = modelo.get_respostas(id_pergunta);
try {
res.render('respostas', {
pergunta: pergunta,
respostas: respostas
});
}
catch(erro) {
res.status(500).json(erro.message);
}
});
app.post('/respostas', (req, res) => {
try {
const id_pergunta = req.body.id_pergunta;
const resposta = req.body.resposta;
modelo.cadastrar_resposta(id_pergunta, resposta);
res.render('resposta-sucesso', {
id_pergunta: id_pergunta
});
}
catch(erro) {
res.status(500).json(erro.message);
}
});
// espera e trata requisições de clientes
const port = 3000;
app.listen(port, 'localhost', () => {
console.log(`ESM Forum rodando na porta ${port}`)
});