-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (92 loc) · 3.29 KB
/
server.js
File metadata and controls
107 lines (92 loc) · 3.29 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
const HOSTNAME = 'localhost';
// MIME types for different file extensions
// Ye browser ko batata hai ki file ka type kya hai
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml'
};
// HTTP Server banao using built-in http module
const server = http.createServer((req, res) => {
console.log(`Request: ${req.method} ${req.url}`);
// API endpoint for signal data
if (req.url === '/api/signal-data' && req.method === 'GET') {
handleApiRequest(req, res);
return;
}
// Static file serving
handleStaticFiles(req, res);
});
// Function to handle API requests (JSON data ko send karne ke liye)
function handleApiRequest(req, res) {
const dataPath = path.join(__dirname, 'public', 'data', 'rsrq_sample_100.json');
// File ko read karo
fs.readFile(dataPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading JSON file:', err);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Failed to load signal data' }));
return;
}
// JSON response send karo
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
res.end(data);
console.log('✅ Sent signal data');
});
}
// Function to handle static files (HTML, CSS, JS files)
function handleStaticFiles(req, res) {
// Root URL ko index.html pe redirect karo
let filePath = req.url === '/' ? '/index.html' : req.url;
filePath = path.join(__dirname, 'public', filePath);
// File extension nikalo to determine MIME type
const extname = path.extname(filePath).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
// Check if file exists
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
// File not found - 404 response
console.log('File not found:', filePath);
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - File Not Found</h1>');
return;
}
// File exists - read and send it
fs.readFile(filePath, (err, content) => {
if (err) {
console.error('Error reading file:', err);
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end('<h1>500 - Internal Server Error</h1>');
return;
}
// Success - send file with correct content type
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
console.log(`✅ Served: ${req.url}`);
});
});
}
// Server start karo
server.listen(PORT, HOSTNAME, () => {
console.log(`🚀 Server running at http://${HOSTNAME}:${PORT}/`);
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n\n👋 Server shutting down...');
server.close(() => {
console.log('✅ Server closed successfully');
process.exit(0);
});
});