Some questions about Learyounode final problem HTTP JSON API Server

Some questions about Learyounode final problem HTTP JSON API Server
0

#1

First off here’s my code so far:

const http = require("http"), bl = require("bl"), url = require("url");

http.createServer(function (req, res) {
  var reqUrl = url.parse(req.url, true);
  var iso = reqUrl.iso;
  var date = new Date(iso);
  var obj = { "hour":date.getHours(), "minute":date.getMinutes(), "second":date.getSeconds()};
  var myJSON = JSON.stringify(obj);
  console.log(myJSON);
  http.get('/api/parsetime', function callback(res) {
    res.pipe(bl(function (err, data) {
      if (err) {
        console.log(err);
      }
      
      console.log(data.toString());
    }));
  });
}).listen(process.argv[2]);

My first question is the assignment requires two endpoints based on the request’s URL pathname. I already wrote a get statement for /api/parsetime but I need one for /api/unixtime as well. Can I just write a second get statement inside the server right after the first statement and will the computer just process whichever statement is applicable?

Second question is where do I place this part of the code:

var reqUrl = url.parse(req.url, true);
  var iso = reqUrl.iso;
  var date = new Date(iso);
  var obj = { "hour":date.getHours(), "minute":date.getMinutes(), "second":date.getSeconds()};
  var myJSON = JSON.stringify(obj);
  console.log(myJSON);

The above code is for the /api/parsetime endpoint code to display the JSON String for debugging but when it does it says ““hour”:null, “minute”:null, “second”:null” so obviously something is wrong with the process where it translates the url to a JSON string somewhere along the way I just haven’t figured out what yet. I don’t want to start messing with it in case it’s a red herring and I should be working on the first question instead.

Last of all, is there a website that simply summarizes the process of making something like this all together? I’ve been trying to find different websites that each tell me part of the code but none of them actually show how to put it together into a cohesive whole at least the ones I’ve found so far.


#2

The assignment says:

Write an HTTP server that serves JSON data when it receives a GET request to the path ‘/api/parsetime’. Expect the request to contain a query string with a key ‘iso’ and an ISO-format time as the value.

It only expects you to write the api server that processes the endpoints , not to write the endpoints themselves. The requests to those endpoints will be automatically written to check if your server is responding correctly, so your job is to check for those requests and respond accordingly.
Hint: you can check what endpoint is being tested with console.log (req.url) , parse it and send the correct response.


#3

Thanks for the reply, I used your advice to figure out the problem yesterday.