codejump
EasyPro challengeJavaScriptTypeScript

Node.js — Your First HTTP Server

Node.jsBackendHTTP

Time for tinyserve to answer HTTP. [`http.createServer(handler)`](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener)
calls handler(req, res) for every request: req.method and req.url say what is asked,
res.writeHead(status, headers) and res.end(body) answer. No framework: Express is built on
exactly these two objects.

Your task

Write createApp() and return the server (do not call listen: the driver does). Routes
depend on the pathname only — ignore the query string:

RequestStatusContent-TypeBody
GET /health200application/json{"status":"ok"}
GET /hello?name=Ada200text/plain; charset=utf-8Hello, Ada! (Hello, world! when name is missing or empty)
another method on /health or /hello405application/json{"error":"Method Not Allowed"} + header Allow: GET
any other path404application/json{"error":"Not Found"}

JSON bodies are JSON.stringify output, without spaces. Paths are case-sensitive.

How the tests run

The sandbox has no TCP network, so the driver starts your server on a Unix socket
(server.listen('/tmp/.../server.sock')) — your code does not change. It sends each test's
requests and reports status, Content-Type, Allow and body.

Sample tests

Test #1Health check
Input: [[{"path":"/health"}]]
Output: [{"body":"{\"status\":\"ok\"}","type":"application/json","status":200}]
Test #2Query parameter and default
Input: [[{"path":"/hello?name=Ada"},{"path":"/hello"}]]
Output: [{"body":"Hello, Ada!","type":"text/plain; charset=utf-8","status":200},{"body":"Hello, world!","type":"text/plain; charset=utf-8","status":200}]
Test #3Unknown path
Input: [[{"path":"/nope"}]]
Output: [{"body":"{\"error\":\"Not Found\"}","type":"application/json","status":404}]
Test #4Wrong method
Input: [[{"path":"/health","method":"DELETE"}]]
Output: [{"body":"{\"error\":\"Method Not Allowed\"}","type":"application/json","allow":"GET","status":405}]