Skip to content

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

Description

@sebasbad

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions