-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (48 loc) · 1.89 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
const express = require('express');
const path = require('path');
const WolframAlphaAPI = require('wolfram-alpha-api');
const app = express();
const port = 3000;
const waApi = WolframAlphaAPI('R73LK6-787HQYGX78');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/includes/cabecalhoNode.html', (req, res) => {
const filename = req.params.filename;
res.sendFile(path.join(__dirname, 'public', 'node', 'includes', 'cabecalhoNode.html'));
});
app.get('/includes/rodapeNode.html', (req, res) => {
const filename = req.params.filename;
res.sendFile(path.join(__dirname, 'public', 'node', 'includes', 'rodapeNode.html'));
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/node/index.html'));
});
app.use(express.json());
app.post('/ask', async (req, res) => {
const question = req.body.question;
try {
const response = await waApi.getFull({
input: question,
podstate: 'Step-by-step solution',
language: 'pt',
excludepodid: 'Input',
width: '700',
mag: '3',
});
const pods = response.pods;
const responseHTML = pods.map((pod) => {
const subpodHTML = pod.subpods.map(subpod =>
`<div class="d-flex justify-content-center">
<img src="${subpod.img.src}" alt="${subpod.img.alt}" class="img-fluid">
</div>`
).join('\n');
return `<p class="col-12 subtextForm">${pod.title}</p>\n${subpodHTML}`;
}).join('\n');
res.json({ responseHTML });
} catch (error) {
console.error('Erro ao processar pergunta:', error);
res.status(500).json({ error: 'Erro ao processar pergunta' });
}
});
app.listen(port, () => {
console.log(`Servidor rodando em http://localhost:${port}`);
});