-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (60 loc) · 2.11 KB
/
Copy pathindex.js
File metadata and controls
71 lines (60 loc) · 2.11 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import http from 'http';
import url from 'url';
import QRCode from 'qrcode';
const PORT = process.env.PORT || 3000;
const server = http.createServer(async (req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
const query = parsedUrl.query;
// Mock endpoint: /v1/create-qr-code/
if (pathname === '/v1/create-qr-code/' && req.method === 'GET') {
const data = query.data;
const size = query.size || '200x200';
if (!data) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing data parameter' }));
return;
}
try {
// Parse size (e.g., "200x200")
const [width, height] = size.split('x').map(Number);
console.log("WH:", width, height)
if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0 || !width || !height) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid size parameter' }));
return;
}
if (width > 10000 || height > 10000) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Size too large' }));
return;
}
// Generate QR code as PNG buffer
const pngBuffer = await QRCode.toBuffer(data, {
width,
height,
margin: 1,
color: {
dark: '#000000',
light: '#FFFFFF',
},
});
console.log(`GENERATED[${new Date()}]:`, data);
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': pngBuffer.length,
});
res.end(pngBuffer);
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: error.message }));
}
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not found' }));
}
});
server.listen(PORT, () => {
console.log(`QR Code API mock server running at http://localhost:${PORT}`);
console.log(`Endpoint: GET /v1/create-qr-code/?size=200x200&data=YOUR_DATA`);
});