Spaces:
Configuration error
Configuration error
File size: 1,105 Bytes
3dee94a | 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 | import express from 'express'
import cors from 'cors'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express()
const PORT = 4000
const SESSIONS_PATH = path.join(__dirname, 'data')
app.use(cors())
app.use(express.json())
if (!fs.existsSync(SESSIONS_PATH)) fs.mkdirSync(SESSIONS_PATH)
app.get('/sessions', (req, res) => {
const files = fs.readdirSync(SESSIONS_PATH)
const sessions = files.map(file => {
const content = fs.readFileSync(path.join(SESSIONS_PATH, file), 'utf-8')
return { id: file.replace('.json', ''), messages: JSON.parse(content) }
})
res.json(sessions)
})
app.post('/sessions/:id', (req, res) => {
const sessionId = req.params.id
const messages = req.body.messages
fs.writeFileSync(path.join(SESSIONS_PATH, `${sessionId}.json`), JSON.stringify(messages, null, 2))
res.json({ status: 'ok' })
})
app.listen(PORT, () => console.log(`✅ Sessões API rodando em http://localhost:${PORT}`))
|