Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
});
|