Update server.js
Browse files
server.js
CHANGED
|
@@ -1,59 +1,22 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
import { render } from '@antv/gpt-vis-ssr';
|
| 3 |
-
import path from 'path';
|
| 4 |
-
import fs from 'fs/promises';
|
| 5 |
-
import crypto from 'crypto';
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
app.use(express.json());
|
| 9 |
|
| 10 |
-
// Serve static files in /images (adjust if you use another folder)
|
| 11 |
-
app.use('/images', express.static(path.join(process.cwd(), 'images')));
|
| 12 |
-
|
| 13 |
app.post('/api/chart', async (req, res) => {
|
| 14 |
try {
|
| 15 |
-
const options = req.body;
|
| 16 |
const vis = await render(options);
|
| 17 |
const buffer = vis.toBuffer();
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
const filename = crypto.randomBytes(16).toString('hex') + '.png';
|
| 21 |
-
const filePath = path.join(process.cwd(), 'images', filename);
|
| 22 |
-
|
| 23 |
-
// Optional: Check if the images directory exists before writing (non-destructive)
|
| 24 |
-
// This helps catch issues without trying to create it.
|
| 25 |
-
const imagesDir = path.join(process.cwd(), 'images');
|
| 26 |
-
await fs.access(imagesDir); // Throws if directory doesn't exist or is inaccessible
|
| 27 |
-
|
| 28 |
-
// If you want to add back a creation check (uncomment if needed):
|
| 29 |
-
// try {
|
| 30 |
-
// await fs.access(imagesDir);
|
| 31 |
-
// } catch {
|
| 32 |
-
// await fs.mkdir(imagesDir, { recursive: true });
|
| 33 |
-
// }
|
| 34 |
-
|
| 35 |
-
// Save image to disk
|
| 36 |
-
await fs.writeFile(filePath, buffer);
|
| 37 |
-
|
| 38 |
-
// Build public URL (HuggingFace Space exposes root as https://<space>.hf.space/)
|
| 39 |
-
const imageUrl = `/images/${filename}`;
|
| 40 |
-
const fullUrl = `${req.protocol}://${req.get('host')}${imageUrl}`;
|
| 41 |
-
|
| 42 |
-
res.json({
|
| 43 |
-
success: true,
|
| 44 |
-
resultObj: fullUrl,
|
| 45 |
-
errorMessage: ''
|
| 46 |
-
});
|
| 47 |
} catch (err) {
|
| 48 |
-
res.json({
|
| 49 |
-
success: false,
|
| 50 |
-
resultObj: '',
|
| 51 |
-
errorMessage: err.message
|
| 52 |
-
});
|
| 53 |
}
|
| 54 |
});
|
| 55 |
|
| 56 |
const port = process.env.PORT || 7860;
|
| 57 |
app.listen(port, () => {
|
| 58 |
console.log('API running on port ' + port);
|
| 59 |
-
});
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
import { render } from '@antv/gpt-vis-ssr';
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
const app = express();
|
| 5 |
app.use(express.json());
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
app.post('/api/chart', async (req, res) => {
|
| 8 |
try {
|
| 9 |
+
const options = req.body; // chart options (type, data, etc.)
|
| 10 |
const vis = await render(options);
|
| 11 |
const buffer = vis.toBuffer();
|
| 12 |
+
res.setHeader('Content-Type', 'image/png');
|
| 13 |
+
res.send(buffer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
} catch (err) {
|
| 15 |
+
res.status(400).json({ error: err.message });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
});
|
| 18 |
|
| 19 |
const port = process.env.PORT || 7860;
|
| 20 |
app.listen(port, () => {
|
| 21 |
console.log('API running on port ' + port);
|
| 22 |
+
});
|