-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (48 loc) · 1.66 KB
/
index.js
File metadata and controls
56 lines (48 loc) · 1.66 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
const http = require('http');
const port = 3000;
function printDateInISOWithTimezone(date) {
const timezoneOffset = date.getTimezoneOffset();
const offsetDate = new Date(date.getTime() - timezoneOffset * 60 * 1000);
const isoString = offsetDate.toISOString();
const timezoneHours = Math.floor(Math.abs(timezoneOffset) / 60);
const timezoneMinutes = Math.abs(timezoneOffset) % 60;
const timezoneSign = timezoneOffset < 0 ? '+' : '-';
const formattedTimezone =
timezoneOffset === 0
? 'Z'
: `${timezoneSign}${String(timezoneHours).padStart(2, '0')}:${String(
timezoneMinutes
).padStart(2, '0')}`;
return `${isoString.substring(0, isoString.length - 1)}${formattedTimezone}`;
}
const server = http.createServer((req, res) => {
// Skip logging for probe endpoint
if (req.url !== '/_probe') {
console.log('\n=== Incoming Request ===');
console.log('Time:', printDateInISOWithTimezone(new Date()));
console.log('Method:', req.method);
console.log('URL:', req.url);
console.log('Headers:');
console.log(JSON.stringify(req.headers, null, 2));
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
});
req.on('end', () => {
body = Buffer.concat(body);
if (body.length) {
console.log(`Body Length: ${body.length}`);
console.log('Body:');
console.log(body.toString());
} else {
console.log('<No body content>');
}
});
}
// Send response
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Request received and logged');
});
server.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});