Return JSON line of code from node.js

I am a complete noob in node.js and am trying to learn it using learnyounode. I am stuck in the last learnyounode issue - [HTTP JSON API SERVER].
Here, the tutorial will call the URL to provide time (as the iso standard), and the node.js server should return the json response to (k, v), where the pair will be k = {"hour", "minute", "second" }.

My solution goes as below -

var http = require('http');
var url = require('url');

function get_json(str) {
        var result = [];
        str = str.substr(str.lastIndexOf('T') + 1);
        result['hour'] = Number(str.substring(0, str.indexOf(':')));
        str = str.substring(str.indexOf(':') + 1);
        result['minute'] = Number(str.substring(0, str.indexOf(':')));
        str = str.substring(str.indexOf(':') + 1);
        result['second'] = Number(str.substring(0, str.indexOf('.')));
        return result;
}

function get_unix(str) {
        var result = get_json(str);
        result['unix'] =  ((result['hour'] * 3600000) +
                (result['min'] * 60000) + (result['sec'] * 1000));
        return result;
}

var server = http.createServer(function (req, res) {
        if (req.method != 'GET') {
                return res.write('{ "error": "query-failed" }');
        }
        var cur_url = url.parse(req.url, true);
        if (cur_url['pathname'] == '/api/parsetime') {
                res.writeHead(200, { 'Content-Type': 'application/json' });
                res.write(JSON.stringify(get_json(cur_url['query']['iso'])));
        } else if (cur_url['pathname'] == '/api/unixtime') {
                res.writeHead(200, { 'Content-Type': 'application/json' });
                res.write(JSON.stringify(get_unix(cur_url['query']['iso'])));
        }
        console.log(get_json(cur_url['query']['iso']));
        console.log(JSON.stringify(get_json(cur_url['query']['iso'])));
        res.end();
});

server.listen(process.argv[2]);

But the solution does not work correctly, because it JSON.stringify()returns an empty string []. What am I missing here?

The output of the current solution:

[ hour: 7, minute: 27, second: 38 ]
[]
[]
[]
[ hour: 7, minute: 27, second: 38 ]
[]
+4
source share
2 answers
function get_json(str) {
        var result = [];
        str = str.substr(str.lastIndexOf('T') + 1);
        result['hour'] = Number(str.substring(0, str.indexOf(':')));
        str = str.substring(str.indexOf(':') + 1);
        result['minute'] = Number(str.substring(0, str.indexOf(':')));
        str = str.substring(str.indexOf(':') + 1);
        result['second'] = Number(str.substring(0, str.indexOf('.')));
        return result;
}

result , . JavaScript , , JSON - , .

(var result = {};), JSON . , JSON .

+4

.

function get_json(str) {
    str = new Date(str).toLocaleTimeString();

    var arr = str.split(":");

    var result = {};
    result['hour'] = +arr[0];
    result['minute'] = +arr[1];
    result['second'] = +arr[2];

    return result;
}

function get_unix(str) {
    return {"unixtime": +(new Date(str))};
}

+ int

+3

All Articles