-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (25 loc) · 873 Bytes
/
Copy pathserver.js
File metadata and controls
30 lines (25 loc) · 873 Bytes
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
const http = require('http')
const fs = require('fs')
const path = require('path')
const LOG_FILE = path.join(__dirname, 'visitors.log')
const PORT = process.env.PORT || 8080
const server = http.createServer((req, res) => {
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress
const ua = req.headers['user-agent'] || 'unknown'
const time = new Date().toISOString()
const ref = req.headers['referer'] || 'direct'
const line = `[${time}] IP: ${ip} | UA: ${ua} | Referer: ${ref}\n`
fs.appendFile(LOG_FILE, line, (err) => {
if (err) console.error('Write error:', err)
})
// CORS for all origins
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain'
})
res.end('ok')
})
server.listen(PORT, () => {
console.log(`Tracker running on port ${PORT}`)
console.log(`Logging to ${LOG_FILE}`)
})