workshopper / learnyounode

Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP JSON API SERVER (Exercise 13 of 13) verify error

sebasbad opened this issue · comments

http-json-api-server.js

'use strict'

const http = require('http')
const URL = require('url').URL

const port = process.argv[2]

function convertISOTimeToJson(url) {
    const date = new Date(url.searchParams.get('iso'))

    if ('/api/parsetime' === url.pathname) {
        return { 
            'hour': date.getHours(), 
            'minute': date.getMinutes(), 
            'second': date.getSeconds()
        }
    }

    if ('/api/unixtime' === url.pathname) {
        return { 
            'unixtime': date.getTime()
        }
    }
}

const server = http.createServer((request, response) => {
    const url = new URL(`${request.scheme}://${request.host}${request.url}`)
    const time = convertISOTimeToJson(url)

    if ('GET' === request.method && time) {
        response.writeHead(200, { 'Content-Type': 'application/json' })
        response.write(JSON.stringify(time))
    } else {
        response.writeHead(404)
    }

    response.end()
})

server.listen(port)

$ learnyounode verify http-json-api-server.js

# LEARN YOU THE NODE.JS FOR MUCH WIN!

## HTTP JSON API SERVER (Exercise 13 of 13)

 ✗ 

 Error connecting to
 (http://localhost:44952/api/parsetime?iso=2020-03-06T19:37:36.894Z):
 socket hang up


Your submission results compared to the expected:

────────────────────────────────────────────────────────────────────────────────

1.  ACTUAL:    "{\"hour\":20,\"minute\":37,\"second\":36}"
1.  EXPECTED:  ""

 ✗ 

 Error connecting to
 (http://localhost:44952/api/unixtime?iso=2020-03-06T19:37:36.894Z):
 connect ECONNREFUSED 127.0.0.1:44952

2.  ACTUAL:    "{\"unixtime\":1583523456894}"
2.  EXPECTED:  

3.  ACTUAL:    ""
3.  EXPECTED:  


────────────────────────────────────────────────────────────────────────────────

 ✗ 

 Submission results did not match expected!

 # FAIL Your solution to HTTP JSON API SERVER didn't pass. Try again!

Hi @sebasbad

Your solution seems correct.

Not sure why the solution process failed. Could you give us details about your os and node version to try replicate the error?

Hi @ccarruitero ,

$ sw_vers

ProductName:    Mac OS X
ProductVersion: 10.15.5
BuildVersion:   19F101

$ node --version

v8.15.1

Uhmm .. could be something specific from node v8. Can you try with a more recent node version (v12 or v14) ?