Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sebasbad opened this issue Mar 6, 2020 · 3 comments
Open

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

sebasbad opened this issue Mar 6, 2020 · 3 comments

Comments

@sebasbad
Copy link

sebasbad commented Mar 6, 2020

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!
@ccarruitero
Copy link
Contributor

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?

@sebasbad
Copy link
Author

Hi @ccarruitero ,

$ sw_vers

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

$ node --version

v8.15.1

@ccarruitero
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants