-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#4): ✨ Made a small express server for saving annotations
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}`) | ||
| }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } | ||
| }) |