-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw_1.js
38 lines (28 loc) · 870 Bytes
/
hw_1.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
var http = require("http"), fs = require("fs");
var events = require('events');
var eventEmitter = new events.EventEmitter();
var server = http.createServer().listen(8888);
var userAgent;
var time;
var unixTime;
var unixDate;
var headers;
server.on('request', function(request, response){
headers = "UNIX Timestamp, User Agent\n";
userAgent = request.headers['user-agent'];
time = new Date;
unixTime = parseInt(time.getTime() / 1000);
unixDate = new Date(unixTime * 1000);
string = unixDate + " , " + userAgent.replace(',', '') + "\n";
fs.exists('hw_1.csv', function(exists){
if(!exists){
fs.writeFile('hw_1.csv', headers, function(err){
if (err) throw (err);
});
}else{
fs.appendFile('hw_1.csv', string, function(err){
if(err) throw(err);
});
}
});
});