-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-time-server.js
More file actions
39 lines (31 loc) · 1.11 KB
/
10-time-server.js
File metadata and controls
39 lines (31 loc) · 1.11 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
// Write a TCP time server!
// Your server should listen to TCP connections on the port provided by the first argument to your program.
// For each connection you must write the current date & 24 hour time in the format:
// "YYYY-MM-DD hh:mm"
// followed by a newline character. Month, day, hour and minute must be zero-filled to 2 integers.
// For example:
// "2013-07-06 17:42"
'use strict';
var net = require('net');
var port = process.argv[2];
var provideTwoDigits = function (someNumber){
if(someNumber < 10){
return parseFloat(someNumber).toFixed(2);
}
else {
return someNumber;
}
};
var myDate = function(){
var d = new Date(Date.now());
return d.getFullYear() + '-' + provideTwoDigits((d.getMonth()+1)) + '-' + provideTwoDigits(d.getDate()) + ' ' + provideTwoDigits(d.getHours()) + ':' + provideTwoDigits(d.getMinutes());
};
var server = net.createServer(function (socket) {
// socket handling logic
socket.write(myDate());
socket.write('\n');
socket.end();
});
server.listen(port, function() { //'listening' listener
//console.log('server bound');
});