-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (32 loc) · 1.15 KB
/
server.js
File metadata and controls
40 lines (32 loc) · 1.15 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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const { getConfiguration } = require('./openai-api');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.post('/setHouseSetup', async (req, res) => {
const description = req.body.description;
try {
const config = await getConfiguration(description);
// Verifica se a resposta é um JSON válido
let configJson;
try {
configJson = JSON.parse(config);
} catch (parseError) {
return res.status(500).json({ error: 'Invalid JSON response from OpenAI API' });
}
fs.writeFileSync(path.join(__dirname, 'public', 'config.json'), JSON.stringify(configJson, null, 2));
res.json(configJson);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});