-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (81 loc) · 2.62 KB
/
server.js
File metadata and controls
94 lines (81 loc) · 2.62 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
// Simple Node.js HTTP server for Albert Heijn Self-Scanner - no dependencies
const http = require('http');
const path = require('path');
const fs = require('fs');
const PORT = process.env.PORT || 8000;
// MIME types mapping
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
return mimeTypes[ext] || 'application/octet-stream';
}
const server = http.createServer((req, res) => {
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
const pathname = parsedUrl.pathname;
// Health check endpoint
if (pathname === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0'
}));
return;
}
// Determine file path
let filePath;
if (pathname === '/') {
filePath = path.join(__dirname, 'index.html');
} else {
filePath = path.join(__dirname, pathname);
}
// Security check - prevent directory traversal
if (!filePath.startsWith(__dirname)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
// Check if file exists
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
res.writeHead(404);
res.end('File not found');
return;
}
// Read and serve the file
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500);
res.end('Server error');
return;
}
const mimeType = getMimeType(filePath);
res.writeHead(200, { 'Content-Type': mimeType });
res.end(data);
});
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`🚀 Albert Heijn Self-Scanner running on http://localhost:${PORT}`);
console.log(`📱 Access the application at http://localhost:${PORT}`);
console.log(`🔍 Health check available at http://localhost:${PORT}/health`);
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n👋 Shutting down Albert Heijn Self-Scanner...');
server.close(() => process.exit(0));
});
process.on('SIGTERM', () => {
console.log('\n👋 Shutting down Albert Heijn Self-Scanner...');
server.close(() => process.exit(0));
});