The Time Server challenge in learnyounode (Node.js)

I am stuck in Node.js challenge part 3 (The Time Server challenge in learnyounode) where I have to write a server that listen on ports and writes the date in a specific format to the data stream.

I have written the function logic inside the server. I know the function logic is correct because I have tested it separately, but for some reason it would not write to the data stream and the output when the server is tested is an empty string everytime.

Here’s my code:

/* 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"                                                                                                                                                                                     
     After sending the string, close the connection.*/

var net = require('net');

var add_zero = function (number){
    return "0" + number.toString();

var string_num = function (year, month, day, hour, minute) {
    var format = year.toString() + '-' + needs_zero(month) + '-' +needs_zero(day) + ' ' + needs_zero(hour) + ':' + needs_zero(minute) + '\n';
    return format;

var needs_zero = function (number) {
    return (number<10) ? add_zero(number) : number.toString();

var server = net.createServer(function (socket){
        var date = new Date();
        var data = string_num(date.getFullYear(),date.getMonth(), date.getDate(), date.getHours(), date.getMinutes());


Any thoughts on what’s wrong with this code?

Have you tried logging your time string? What does it look like? Are you remembering to include the new line char after thie string?

Also what about socket.close() instead of socket.end()? (just a guess)

delete this line from the connect handler


it’s a race condition - the server may end the connection before the write completes

instead end the connection in the write complete handler

socket.write(data, ()=>{