Node.js — Your First HTTP Server
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:
| Request | Status | Content-Type | Body |
|---|---|---|---|
GET /health | 200 | application/json | {"status":"ok"} |
GET /hello?name=Ada | 200 | text/plain; charset=utf-8 | Hello, Ada! (Hello, world! when name is missing or empty) |
another method on /health or /hello | 405 | application/json | {"error":"Method Not Allowed"} + header Allow: GET |
| any other path | 404 | application/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