Skip to content

Commit

Permalink
feat(#4): ✨ Made a small express server for saving annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
gautegf committed Oct 20, 2025
1 parent 6cf61d1 commit 89d65f4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
48 changes: 48 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 = process.env.PORT || 5174

app.use(cors())
app.use(express.json({ limit: '2mb' }))

// Directory where annotations are stored alongside Vite's public folder
const annotationsDir = path.join(dirname, 'public', 'annotations')
const annotationsFile = path.join(annotationsDir, 'annotations.json')

// Ensure directory exists
function ensureDirSync(dir) {
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
}

// Save annotations JSON
app.post('/api/annotations', (req, res) => {
try {
const body = req.body
if (!body || typeof body !== 'object') {
return res.status(400).json({ error: 'Invalid JSON body' })
}
if (!body.folders || typeof body.folders !== 'object') {
return res.status(400).json({ error: 'Missing folders in payload' })
}

ensureDirSync(annotationsDir)
const data = JSON.stringify(body, null, 2)
fs.writeFileSync(annotationsFile, data, 'utf8')
return res.json({ saved: true, path: '/annotations/annotations.json' })
} catch (err) {
console.error('Failed to save annotations:', err)
return res.status(500).json({ error: 'Failed to save annotations' })
}
})

app.listen(PORT, () => {
console.log(`API server listening on http://localhost:${PORT}`)
})
14 changes: 14 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from 'vite'

export default defineConfig({
server: {
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:5174',
changeOrigin: true,
secure: false
}
}
}
})

0 comments on commit 89d65f4

Please sign in to comment.