-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (86 loc) · 3.63 KB
/
Copy pathserver.js
File metadata and controls
100 lines (86 loc) · 3.63 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
const http = require('http');
const fs = require('fs');
const url = require('url');
const querystring = require('querystring');
// Store messages in memory (in a real application, you'd want to use a database)
let messages = [];
// Create the HTTP server
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
// Set CORS headers for all responses
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
// Handle preflight requests
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
// Handle message submission (POST request)
if (req.method === 'POST' && parsedUrl.pathname === '/send') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const formData = querystring.parse(body);
const message = formData.message?.trim();
if (message) {
// Add timestamp to message
const messageWithTimestamp = {
text: message,
timestamp: new Date().toISOString(),
id: Date.now() // Unique ID for each message
};
messages.push(messageWithTimestamp);
// Log message to file
const logMessage = `[${messageWithTimestamp.timestamp}] Message: ${message}\n`;
fs.appendFile('chat-log.txt', logMessage, (err) => {
if (err) console.error('Error writing to log file:', err);
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, message: messageWithTimestamp }));
} else {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, error: 'Message cannot be empty' }));
}
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, error: 'Internal server error' }));
}
});
}
// Handle fetching messages (GET request)
else if (req.method === 'GET' && parsedUrl.pathname === '/messages') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(messages));
}
// Serve the homepage
else if (parsedUrl.pathname === '/') {
try {
const responseHtml = fs.readFileSync('index.html', 'utf8');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(responseHtml);
} catch (err) {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end('<h1>500 Internal Server Error</h1><p>Error loading the page. Please try again later.</p>');
}
}
// Handle unknown routes
else {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1><p>The page you are looking for does not exist. Please go back to <a href="/">the homepage</a>.</p>');
}
});
// Error handling for the server
server.on('error', (error) => {
console.error('Server error:', error);
});
// Start the server
const PORT = process.env.PORT || 3277;
const HOST = '0.0.0.0';
server.listen(PORT, HOST, () => {
console.log(`Server running at http://${HOST}:${PORT}/`);
});