Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (55 loc) · 1.82 KB
/
Copy pathserver.js
File metadata and controls
67 lines (55 loc) · 1.82 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
import express from 'express';
import cors from 'cors';
import { generateTicket, validateTicket } from './build/src/main.js';
import fs from 'fs';
import path from 'path';
const app = express();
const PORT = 4242;
app.use(cors());
app.use(express.json());
app.use(express.static('frontend'));
app.use('/img', express.static('img'));
app.post('/api/generate-ticket', async (req, res) => {
try {
const result = await generateTicket();
res.json({
success: true,
publicKey: result.publicKey,
qrPath: `/img/${result.publicKey}.bmp`
});
} catch (error) {
console.error('Error generating ticket:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/validate-ticket', async (req, res) => {
try {
const { imagePath } = req.body;
if (!imagePath) {
return res.status(400).json({ success: false, error: 'Image path is required' });
}
const status = await validateTicket(imagePath);
res.json({ success: true, status });
} catch (error) {
console.error('Error validating ticket:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/upload-image', express.raw({ limit: '10mb', type: 'image/*' }), async (req, res) => {
try {
const uploadDir = path.join(process.cwd(), 'uploads');
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir);
}
const filename = `ticket-${Date.now()}.bmp`;
const filepath = path.join(uploadDir, filename);
fs.writeFileSync(filepath, req.body);
res.json({ success: true, filepath });
} catch (error) {
console.error('Error uploading image:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});