-
Notifications
You must be signed in to change notification settings - Fork 1
/
logNode.js
38 lines (32 loc) · 1.12 KB
/
logNode.js
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
// save logs in a dedicated folder in CSV format
var http = require('http');
var fs = require('fs-extra');
http.createServer(function (request, response) {
writeIP(request.socket.remoteAddress);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8081);
function writeIP(requestIP) {
var date = new Date();
var day = date.getDate().toString(10).padStart(2, '0')
var month = date.getMonth().toString(10).padStart(2, '0');
var year = date.getFullYear().toString(10);
var fullDate = day+month+year;
// append to file
if (fs.existsSync('logs/'+month+year+'.log')) {
fs.appendFile("./logs/" + month+year + ".log",
requestIP+', '+fullDate+ '\n',
error => {
if (error) console.log(error);
});
}
// else create and write to file
if (!fs.existsSync('logs/'+month+year+'.log')) {
fs.writeFile("./logs/" + month+year + ".log",
requestIP+', '+fullDate + '\n',
error => {
if (error) console.log(error);
});
}
}
server.listen(80);