-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
33 lines (28 loc) · 987 Bytes
/
app.js
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
const express = require('express');
const sharp = require('sharp');
const app = express();
const port = 3000;
// Middleware to handle JSON data
app.use(express.json());
app.post('/convert-svg', async (req, res) => {
try {
// Expecting SVG data as a string in the JSON body under the key 'svg'
const svgString = req.body.svg;
if (!svgString) {
return res.status(400).send('No SVG data provided.');
}
// Convert SVG string to PNG
const pngBuffer = await sharp(Buffer.from(svgString))
.png()
.toBuffer();
// Send the PNG image as response
res.set('Content-Type', 'image/png');
res.send(pngBuffer);
} catch (error) {
console.error('Error converting image:', error);
res.status(500).send('Failed to convert image.');
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});