-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (78 loc) · 3 KB
/
Copy pathserver.js
File metadata and controls
95 lines (78 loc) · 3 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import express from 'express';
import QRCode from 'qrcode';
import cors from 'cors';
import { config } from 'dotenv';
import process from 'process';
import path from 'path';
import { fileURLToPath } from 'url';
// Load environment variables from .env file
config();
// ES module __dirname equivalent
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(express.json());
app.use(cors());
// Route to generate QR code
app.post('/api/generate', async (req, res) => {
try {
const { text, color, bgColor, size, errorCorrectionLevel } = req.body;
// Validate inputs
if (!text) {
return res.status(400).json({ error: 'Text is required' });
}
// Validate color formats if provided
if (color && !/^#[0-9A-F]{6}$/i.test(color)) {
return res.status(400).json({ error: 'Invalid color format. Use hexadecimal format (e.g. #FF0000)' });
}
if (bgColor && !/^#[0-9A-F]{6}$/i.test(bgColor)) {
return res.status(400).json({ error: 'Invalid background color format. Use hexadecimal format (e.g. #FFFFFF)' });
}
// Validate size
if (size && (isNaN(size) || size < 100 || size > 1000)) {
return res.status(400).json({ error: 'Size must be a number between 100 and 1000' });
}
// Validate error correction level
const validErrorLevels = ['L', 'M', 'Q', 'H'];
if (errorCorrectionLevel && !validErrorLevels.includes(errorCorrectionLevel)) {
return res.status(400).json({
error: `Invalid error correction level. Must be one of: ${validErrorLevels.join(', ')}`
});
}
// Options for QR code generation
const options = {
color: {
dark: color || '#000000',
light: bgColor || '#ffffff'
},
width: size || 300,
errorCorrectionLevel: errorCorrectionLevel || 'M',
margin: 1
};
// Generate QR code as data URL
const qrDataURL = await QRCode.toDataURL(text, options);
res.json({ qrCode: qrDataURL });
} catch (error) {
console.error('Error generating QR code:', error);
// More specific error handling
if (error.name === 'TypeError') {
res.status(400).json({ error: 'Invalid QR code parameters' });
} else {
res.status(500).json({ error: 'Failed to generate QR code: ' + error.message });
}
}
});
// Serve static files from the React app in production
if (process.env.NODE_ENV === 'production') {
// Serve static files
app.use(express.static(path.join(__dirname, 'dist')));
// Handle React routing, return all requests to React app
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
}
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});