-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (44 loc) · 1.66 KB
/
server.js
File metadata and controls
54 lines (44 loc) · 1.66 KB
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
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const app = express();
const db = new sqlite3.Database(path.join(__dirname, 'mathfly.db'));
// Para servir arquivos estáticos (HTML, CSS, JS)
app.use(express.static(path.join(__dirname)));
// Rota para buscar várias perguntas aleatórias
app.get('/api/question/:difficulty/:count', (req, res) => {
const difficulty = req.params.difficulty;
const count = parseInt(req.params.count) || 1;
console.log(`Dificuldade: ${difficulty}, Contagem: ${count}`);
db.all(`SELECT * FROM questions_${difficulty} ORDER BY RANDOM() LIMIT ?`, [count], (err, rows) => {
if (err) {
console.error(err.message);
return res.status(500).json({ error: 'Erro ao buscar as perguntas.' });
}
if (rows.length === 0) {
return res.status(404).json({ error: 'Nenhuma pergunta encontrada.' });
}
res.json(rows);
});
});
// Rota para carregar o arquivo HTML de acordo com a dificuldade
app.get('/nivel', (req, res) => {
res.sendFile(path.join(__dirname, 'nivel.html'));
});
app.get('/nivel_2', (req, res) => {
res.sendFile(path.join(__dirname, 'nivel_2.html'));
});
app.get('/nivel_3', (req, res) => {
res.sendFile(path.join(__dirname, 'nivel_3.html'));
});
app.get('/nivel_4', (req, res) => {
res.sendFile(path.join(__dirname, 'nivel_4.html'));
});
// Rota inicial mostra fases.html
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'fases.html'));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Servidor rodando na porta ${PORT}`);
});