-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (26 loc) · 968 Bytes
/
index.js
File metadata and controls
33 lines (26 loc) · 968 Bytes
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 QRCode = require('qrcode');
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 3000;
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/generate', async (req, res) => {
const { url } = req.query;
if (!url) return res.status(400).json({ error: 'URL не указан.' });
try {
const options = { width: 400, margin: 2 };
// Генерируем оба формата параллельно
const [png, svg] = await Promise.all([
QRCode.toDataURL(url, options), // PNG в формате Base64
QRCode.toString(url, { ...options, type: 'svg' }) // SVG как XML-строка
]);
res.json({ png, svg });
} catch (err) {
res.status(500).json({ error: 'Ошибка генерации' });
}
});
app.listen(PORT, () => {
console.log(`Сервер: http://localhost:${PORT}`);
});