-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
165 lines (142 loc) · 4.56 KB
/
Copy pathserver.js
File metadata and controls
165 lines (142 loc) · 4.56 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const QRCode = require('qrcode');
const app = express();
const port = process.env.PORT || 8000;
// Create uploads directory if it doesn't exist
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
// Setup storage for multer
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, uploadsDir);
},
filename: function(req, file, cb) {
const fileExt = path.extname(file.originalname);
cb(null, `${uuidv4()}${fileExt}`);
}
});
const upload = multer({ storage: storage });
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// In-memory database for demo purposes (use a real database for production)
const driversDB = [];
// API endpoints
// Get all drivers
app.get('/api/drivers', (req, res) => {
res.json({
success: true,
drivers: driversDB.map(driver => ({
id: driver.id,
numberPlate: driver.numberPlate,
driverName: driver.driverName
}))
});
});
// Get driver by ID
app.get('/api/drivers/:id', (req, res) => {
const driver = driversDB.find(d => d.id === req.params.id);
if (!driver) {
return res.status(404).json({
success: false,
message: 'Driver not found'
});
}
res.json({
success: true,
driver: {
id: driver.id,
numberPlate: driver.numberPlate,
driverName: driver.driverName,
rcBookUrl: `/uploads/${path.basename(driver.rcBook)}`,
licenseUrl: `/uploads/${path.basename(driver.license)}`,
insuranceUrl: `/uploads/${path.basename(driver.insurance)}`
}
});
});
// Create new driver
app.post('/api/drivers', upload.fields([
{ name: 'rcBook', maxCount: 1 },
{ name: 'license', maxCount: 1 },
{ name: 'insurance', maxCount: 1 }
]), (req, res) => {
try {
// Extract form data
const { numberPlate, driverName } = req.body;
// Validate required fields
if (!numberPlate || !driverName || !req.files.rcBook || !req.files.license || !req.files.insurance) {
return res.status(400).json({
success: false,
message: 'All fields are required'
});
}
// Generate unique ID
const id = uuidv4();
// Create driver object
const driver = {
id,
numberPlate,
driverName,
rcBook: req.files.rcBook[0].path,
license: req.files.license[0].path,
insurance: req.files.insurance[0].path,
createdAt: new Date()
};
// Save driver to database
driversDB.push(driver);
// Generate QR code for the driver ID
const qrCodePath = path.join(uploadsDir, `qr_${id}.png`);
QRCode.toFile(qrCodePath, id, {
color: {
dark: '#000000',
light: '#ffffff'
}
});
// Return success response
res.json({
success: true,
id: id,
message: 'Driver information saved successfully'
});
} catch (error) {
console.error('Error saving driver:', error);
res.status(500).json({
success: false,
message: 'An error occurred while saving driver information'
});
}
});
// Get QR code image
app.get('/api/qrcode/:id', (req, res) => {
const qrCodePath = path.join(uploadsDir, `qr_${req.params.id}.png`);
if (fs.existsSync(qrCodePath)) {
res.sendFile(qrCodePath);
} else {
// Generate QR code on-the-fly if file doesn't exist
QRCode.toBuffer(req.params.id, (err, buffer) => {
if (err) {
return res.status(500).json({
success: false,
message: 'Failed to generate QR code'
});
}
res.set('Content-Type', 'image/png');
res.send(buffer);
});
}
});
// Serve uploads
app.use('/uploads', express.static(uploadsDir));
// Serve the main HTML file for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});